-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-replace.sh
executable file
·181 lines (134 loc) · 3.44 KB
/
find-replace.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash
#---
# add other filter functions here
#---
if [[ "$OSTYPE" == "darwin"* ]]; then
SED=gsed
else
SED=sed
fi
function sfilter_shell()
{
find . -type f \( -name '*.sh' \) -print0 2>/dev/null
}
function sfilter_java()
{
find . -type f \( -name '*.java' \) -print0 2>/dev/null
}
function sfilter_cpp() {
find . -type f \( -name '*.cpp' -o -name '*.cxx' -o -name '*.hpp' -o -name '*.hxx' -o -name '*.h' \) -print0 2>/dev/null
}
function sfilter_go() {
find . -type f \( -name '*.go' -o -name '*.mod' \) -print0 2>/dev/null
}
function sfilter_git() {
git status . 2>&1 >/dev/null
if [ $? != 0 ]; then
echo "Error: current directory is not part of a git tree"
exit 1
fi
git ls-files -z
}
function sfilter_py() {
find . -type f \( -name '*.py' -o -name '*.py3' \) -print0 2>/dev/null
}
show_filters() {
declare -F | awk '{ print $3 }' | grep sfilter_ | $SED -e 's/sfilter_\(.*\)/\1/g' | tr '\n' ' '
}
function Help {
if [[ $1 != "" ]]; then
echo "Error: $*"
fi
if [[ ! -z "${SHORT_HELP_MODE}" ]]; then
echo "-s <source filter> -f <from> -t <to> [-v -h] : find replace in multiple files"
exit 1
fi
cat <<EOF
$0 -s <source filter> -f <from> -t <to> [-v -h]
apply replace to multiple input ifles
-s <source filter> : specify input files; available values: $(show_filters)
-f <from> : replace from
-t <to> : replace to
-r : report how many files were changed.
source filter runs find and then it pipes it into $SED to replace it.
EOF
exit 1
}
REPORT_CHANGES=true
while getopts "hvrs:f:t:" opt; do
case ${opt} in
h)
Help
;;
s)
SOURCE_FILES="$OPTARG"
;;
f)
FROM="$OPTARG"
;;
t)
TO="$OPTARG"
;;
v)
set -x
export PS4='+(${BASH_SOURCE}:${LINENO})'
VERBOSE=1
;;
# r)
# REPORT_CHANGES=true
# ;;
*)
Help "Invalid option"
;;
esac
done
if [[ "$FROM" == "" ]]; then
Help "No -f option"
fi
if [[ "$TO" == "" ]]; then
Help "No -t option"
fi
if [[ "$SOURCE_FILES" == "" ]]; then
Help "No -s option"
fi
function escape_me {
local TMP="$1"
TMP=$(echo $TMP | $SED -e 's/#/\#/g' -e 's# #\\ #g' -e 's#&#\\&#g')
echo "$TMP"
}
FROM=$(escape_me "$FROM")
TO=$(escape_me "$TO")
FILTER="sfilter_${SOURCE_FILES}"
echo $(show_filters) | grep $SOURCE_FILES >/dev/null
if [ $? != 0 ]; then
Help "Filter ${SOURCE_FILES} does not exit. add a function sfilter_${SOURCE_FILES} to $0"
fi
function just_find_and_replace()
{
# that one does the job, but one doesn't have an indication of how many files changed.
$FILTER | xargs -0 $SED -i 's#'"$FROM"'#'"$TO"'#g'
}
function find_replace_and_report_substitutions() {
local foundfiles
local substitutedfiles
local test_file=$(mktemp /tmp/check-find-replace.XXXXXX)
trap 'rm -f '"${test_file}"'' INT TERM HUP EXIT
foundfiles=0
substitutedfiles=0
while IFS= read -r -d '' line; do
$SED -i -e 's#'"$FROM"'#'"$TO"'#w '$test_file'' $line
if [ -s $test_file ]; then
((substitutedfiles+=1))
fi
((foundfiles+=1))
done < <($FILTER)
cat <<EOF
files modified: ${substitutedfiles}
files found by filter: ${foundfiles}
EOF
}
if [ "$REPORT_CHANGES" == "true" ]; then
find_replace_and_report_substitutions
else
just_find_and_replace
fi