-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecrep.sh
executable file
·56 lines (48 loc) · 1.08 KB
/
recrep.sh
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
#!/bin/sh
# SPDX-License-Identifier: MIT
usage () {
echo "usage: $(basename $0) OLD NEW [-s|-w] [-f \"FILE(S)\"]"
}
if [ $# -lt 2 ]; then
usage
exit 1
fi
uname -s | grep -q NT && sedargs="-b"
old=$1
shift
new=$1
shift
files=""
while getopts ":hswf:" option; do
case "$option" in
f) files="$OPTARG"
;;
s) sim="true"
;;
w) word="true"
;;
h) usage
exit 0
;;
:) echo "Error: -$option requires an argument"
usage
exit 1
;;
?) echo "Error: unknown option -$option"
usage
exit 1
;;
esac
done
if [ -z "$files" ]; then
files=$(find . -type f \( ! -regex '.*/\..*' \) \( ! -regex '.*/#.*' \) | \
xargs grep $([ $word ] && echo "-w") -I -l $old 2>/dev/null)
fi
for f in $files; do
[ "$sim" ] && SIM="[SIM] "
echo "${SIM}processing: $f"
if [ -z "$sim" ]; then
[ "$word" ] && old='\b'$old'\b'
sed -i.back $sedargs "s:$old:$new:g" "$f" && rm "$f.back"
fi
done