-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathml-replace.sh
More file actions
75 lines (62 loc) · 2.27 KB
/
ml-replace.sh
File metadata and controls
75 lines (62 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
#
# Multiline replace tool using sed.
#
readonly progname=$(basename $0)
# Display help message
getHelp() {
cat << USAGE >&2
Usage: $progname [search] [replace] [input]
Examples:
ml-replace.sh "$search" "$replace" input.txt > output.txt
USAGE
}
# At least three parameters areneeded
if [ $# -ne 3 ]; then
getHelp
exit 1
fi
#
search="$1"
replace="$2"
input="${3:--}"
# SYNOPSIS
# quoteSearch <text>
#
# DESCRIPTION
# Quotes (escapes) the specified literal text for use in a regular expression,
# whether basic or extended - should work with all common flavors.
#
# sed doesn't support use of literal strings as replacement strings - it invariably
# interprets special characters/sequences in the replacement string.
quoteSearch() {
sed -e 's/[^^]/[&]/g; s/\^/\\^/g; $!a\'$'\n''\\n' <<<"$1" | tr -d '\n';
}
# SYNOPSIS
# quoteReplace <text>
#
# DESCRIPTION
# Quotes (escapes) the specified literal string for safe use as the substitution
# string (the 'new' in `s/old/new/`).
#
# The search string literal must be escaped in a way that its characters aren't
# mistaken for special regular-expression characters.
quoteReplace() {
IFS= read -d '' -r < <(sed -e ':a' -e '$!{N;ba' -e '}' -e 's/[&/\]/\\&/g; s/\n/\\&/g' <<<"$1")
printf %s "${REPLY%$'\n'}"
}
# -> The newlines in multi-line input strings must be translated to '\n' strings,
# which is how newlines are encoded in a regex.
# -> $!a\'$'\n''\\n' appends string '\n' to every output line but the last (the
# last newline is ignored, because it was added by <<<)
# -> tr -d '\n then removes all actual newlines from the string (sed adds one
# whenever it prints its pattern space), effectively replacing all newlines
# in the input with '\n' strings.
# -> -e ':a' -e '$!{N;ba' -e '}' is the POSIX-compliant form of a sed idiom that
# reads all input lines a loop, therefore leaving subsequent commands to operate
# on all input lines at once.
#
# Sources from:
# https://stackoverflow.com/questions/29613304/is-it-possible-to-escape-regex-metacharacters-reliably-with-sed/29613573#29613573
# https://stackoverflow.com/questions/24890230/sed-special-characters-handling/24914337#24914337
sed -e ':a' -e '$!{N;ba' -e '}' -e "s/$(quoteSearch "$search")/$(quoteReplace "$replace")/" "$input"