diff options
author | osm0sis <osm0sis@outlook.com> | 2016-10-12 13:59:45 -0300 |
---|---|---|
committer | osm0sis <osm0sis@outlook.com> | 2016-10-12 14:06:14 -0300 |
commit | 641a1b5a0972d848e854092bba8cf2ba86757c11 (patch) | |
tree | 025573234f4100b35c40d7a2e52563294aa23d10 /anykernel.sh | |
parent | 59c1c5d5964ad09984ff40bdd495eaa7f1245f28 (diff) |
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
Diffstat (limited to 'anykernel.sh')
-rwxr-xr-x | anykernel.sh | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/anykernel.sh b/anykernel.sh index 37478c7..9500a2a 100755 --- a/anykernel.sh +++ b/anykernel.sh @@ -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> |