From 915cdaff1bf95af63bef1e82c23f1b5f9edfc762 Mon Sep 17 00:00:00 2001 From: osm0sis Date: Sat, 14 Jul 2018 16:21:14 -0300 Subject: [PATCH] AK2: fix replace_section and remove_section functions - was broken with search strings containing paths (a limitation with busybox sed) - now matches blank lines and lines containing only white space for next-empty-line matching - include end-of-file as the final possible end-point for next-empty-line matching --- tools/ak2-core.sh | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/tools/ak2-core.sh b/tools/ak2-core.sh index 916e741..3296751 100755 --- a/tools/ak2-core.sh +++ b/tools/ak2-core.sh @@ -354,17 +354,19 @@ replace_string() { # replace_section replace_section() { - local begin endstr end; + local begin endstr last end; begin=`grep -n "$2" $1 | head -n1 | cut -d: -f1`; if [ "$begin" ]; then - test "$3" == " " -o -z "$3" && endstr='^$' || endstr="$3"; - for end in `grep -n "$endstr" $1 | cut -d: -f1`; do + if [ "$3" == " " -o -z "$3" ]; then + endstr='^[[:space:]]*$'; + last=$(wc -l $1 | cut -d\ -f1); + else + endstr="$3"; + fi; + for end in $(grep -n "$endstr" $1 | cut -d: -f1) $last; do if [ "$end" ] && [ "$begin" -lt "$end" ]; then - if [ "$3" == " " -o -z "$3" ]; then - sed -i "/${2//\//\\/}/,/^\s*$/d" $1; - else - sed -i "/${2//\//\\/}/,/${3//\//\\/}/d" $1; - fi; + sed -i "${begin},${end}d" $1; + test "$end" == "$last" && echo >> $1; sed -i "${begin}s;^;${4}\n;" $1; break; fi; @@ -374,17 +376,18 @@ replace_section() { # remove_section remove_section() { - local begin endstr end; + local begin endstr last end; begin=`grep -n "$2" $1 | head -n1 | cut -d: -f1`; if [ "$begin" ]; then - test "$3" == " " -o -z "$3" && endstr='^$' || endstr="$3"; - for end in `grep -n "$endstr" $1 | cut -d: -f1`; do + if [ "$3" == " " -o -z "$3" ]; then + endstr='^[[:space:]]*$'; + last=$(wc -l $1 | cut -d\ -f1); + else + endstr="$3"; + fi; + for end in $(grep -n "$endstr" $1 | cut -d: -f1) $last; do if [ "$end" ] && [ "$begin" -lt "$end" ]; then - if [ "$3" == " " -o -z "$3" ]; then - sed -i "/${2//\//\\/}/,/^\s*$/d" $1; - else - sed -i "/${2//\//\\/}/,/${3//\//\\/}/d" $1; - fi; + sed -i "${begin},${end}d" $1; break; fi; done;