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
This commit is contained in:
osm0sis committed 2016-10-12 14:06:14 -03:00
1 parent 59c1c5d596
commit 641a1b5a09
1 file changed
+15 -4
+15 -4
View File
@@ -121,14 +121,25 @@ replace_string() {
# replace_section <file> <begin search string> <end search string> <replacement string>
replace_section() {
line=`grep -n "$2" $1 | head -n1 | cut -d: -f1`;
sed -i "/${2//\//\\/}/,/${3//\//\\/}/d" $1;
sed -i "${line}s;^;${4}\n;" $1;
begin=`grep -n "$2" $1 | head -n1 | cut -d: -f1`;
for end in `grep -n "$3" $1 | cut -d: -f1`; do
if [ "$begin" -lt "$end" ]; then
sed -i "/${2//\//\\/}/,/${3//\//\\/}/d" $1;
sed -i "${begin}s;^;${4}\n;" $1;
break;
fi;
done;
}
# remove_section <file> <begin search string> <end search string>
remove_section() {
sed -i "/${2//\//\\/}/,/${3//\//\\/}/d" $1;
begin=`grep -n "$2" $1 | head -n1 | cut -d: -f1`;
for end in `grep -n "$3" $1 | cut -d: -f1`; do
if [ "$begin" -lt "$end" ]; then
sed -i "/${2//\//\\/}/,/${3//\//\\/}/d" $1;
break;
fi;
done;
}
# insert_line <file> <if search string> <before|after> <line match string> <inserted line>