Skip to content

Commit 641a1b5

Browse files
committed
AK2: make section functions less potentially destructive
- still unable to do an actual tweak check due to grep being limited to single lines, but.. - have them check to at least ensure there's a match for end string that comes after begin string so the sed should function without any chance of removing more than intended
1 parent 59c1c5d commit 641a1b5

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

anykernel.sh

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,25 @@ replace_string() {
121121

122122
# replace_section <file> <begin search string> <end search string> <replacement string>
123123
replace_section() {
124-
line=`grep -n "$2" $1 | head -n1 | cut -d: -f1`;
125-
sed -i "/${2//\//\\/}/,/${3//\//\\/}/d" $1;
126-
sed -i "${line}s;^;${4}\n;" $1;
124+
begin=`grep -n "$2" $1 | head -n1 | cut -d: -f1`;
125+
for end in `grep -n "$3" $1 | cut -d: -f1`; do
126+
if [ "$begin" -lt "$end" ]; then
127+
sed -i "/${2//\//\\/}/,/${3//\//\\/}/d" $1;
128+
sed -i "${begin}s;^;${4}\n;" $1;
129+
break;
130+
fi;
131+
done;
127132
}
128133

129134
# remove_section <file> <begin search string> <end search string>
130135
remove_section() {
131-
sed -i "/${2//\//\\/}/,/${3//\//\\/}/d" $1;
136+
begin=`grep -n "$2" $1 | head -n1 | cut -d: -f1`;
137+
for end in `grep -n "$3" $1 | cut -d: -f1`; do
138+
if [ "$begin" -lt "$end" ]; then
139+
sed -i "/${2//\//\\/}/,/${3//\//\\/}/d" $1;
140+
break;
141+
fi;
142+
done;
132143
}
133144

134145
# insert_line <file> <if search string> <before|after> <line match string> <inserted line>

0 commit comments

Comments
 (0)