-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathreplace-literal
More file actions
executable file
·122 lines (106 loc) · 2.71 KB
/
replace-literal
File metadata and controls
executable file
·122 lines (106 loc) · 2.71 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
# Display usage information
usage() {
cat <<EOF
Usage: $(basename "$0") -f|--from <string> -t|--to <string> [<file>...]
Replace literal string occurrences in files or stdin (not regex).
Options:
-f, --from <string> String to search for (literal)
-t, --to <string> String to replace with (literal)
-h, --help Display this help message
Arguments:
<file>... Files to process (optional, reads from stdin if omitted)
Examples:
$(basename "$0") -f 'foo' -t 'bar' file1.txt file2.txt
echo 'foo bar' | $(basename "$0") -f 'foo' -t 'bar'
EOF
exit "${1:-0}"
}
# Initialize variables
from_string=""
to_string=""
files=()
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-f | --from)
if [[ -z "${2:-}" ]]; then
echo "Error: --from requires an argument" >&2
usage 1
fi
from_string="$2"
shift 2
;;
-t | --to)
if [[ -z "${2:-}" ]]; then
echo "Error: --to requires an argument" >&2
usage 1
fi
to_string="$2"
shift 2
;;
-h | --help)
usage 0
;;
-*)
echo "Error: Unknown option: $1" >&2
usage 1
;;
*)
files+=("$1")
shift
;;
esac
done
# Validate required arguments
if [[ -z "$from_string" ]]; then
echo "Error: --from is required" >&2
usage 1
fi
if [[ -z "$to_string" ]]; then
echo "Error: --to is required" >&2
usage 1
fi
# Find a delimiter character that doesn't appear in from_string or to_string
# Try common delimiters in order of preference
delimiter=""
for char in '/' '#' '@' '|' '~' '%' ',' ':' ';' '!' '^' '-' '_' '='; do
if [[ "$from_string" != *"$char"* && "$to_string" != *"$char"* ]]; then
delimiter="$char"
break
fi
done
if [[ -z "$delimiter" ]]; then
echo "Error: Cannot find a safe delimiter character for the given strings" >&2
exit 1
fi
# Process stdin if no files specified
if [[ ${#files[@]} -eq 0 ]]; then
# Use perl with \Q...\E for literal string replacement
# -pe: print each line after processing
# \Q...\E: quote (disable) pattern metacharacters for literal matching
perl -pe "s${delimiter}\Q$from_string\E${delimiter}$to_string${delimiter}g"
exit 0
fi
# Process each file
for file in "${files[@]}"; do
if [[ ! -f "$file" ]]; then
echo "Error: File not found: $file" >&2
exit 1
fi
if [[ ! -r "$file" ]]; then
echo "Error: Cannot read file: $file" >&2
exit 1
fi
if [[ ! -w "$file" ]]; then
echo "Error: Cannot write to file: $file" >&2
exit 1
fi
# Use perl with \Q...\E for literal string replacement
# -i: edit in place
# -pe: print each line after processing
# \Q...\E: quote (disable) pattern metacharacters for literal matching
perl -i -pe "s${delimiter}\Q$from_string\E${delimiter}$to_string${delimiter}g" "$file"
done