diff options
author | Einhard Leichtfuß <alguien@respiranto.de> | 2018-12-23 05:58:13 +0100 |
---|---|---|
committer | Einhard Leichtfuß <alguien@respiranto.de> | 2018-12-23 05:58:13 +0100 |
commit | 564a3e2ec83e3883cafe9efd2c6f88144a0835db (patch) | |
tree | f6de746330e59d978ba9767dfc452e9ff5b447d3 /basic.sh | |
parent | 8427f2f38448129c69cc1b0f579296201003909f (diff) |
Simplify basic.sh:replace_line and switch to EREs
EREs: extended regular expressions in contrast to basic regular
expressions (BREs).
Diffstat (limited to 'basic.sh')
-rw-r--r-- | basic.sh | 32 |
1 files changed, 20 insertions, 12 deletions
@@ -3,6 +3,8 @@ # Copyright 2018 Einhard Leichtfuß # +# All methods operate on the file "$tmp". + typeset -a langs langs=( fra-deu deu-fra @@ -31,37 +33,43 @@ function cleanup } trap cleanup EXIT -# $1: string to replace: regular expression +# $1: string to replace: extended regular expression # $2: string to replace with: sed replacement string function replace { - sed -i "s/${1}/${2}/g" "$tmp" + sed -Ei "s/${1}/${2}/g" "$tmp" } -# $1 line to replace: regular expression +# Replace line by a string (possibly including newlines). +# $1 line to replace: extended regular expression # $2 text to replace with: (newline terminated) sed replacement string # $3 removal type: (rm-plus-one|normal) +# `- If $2 is empty, whether to remove only that line or also the one +# following. function replace_line { if test -n "$2" then - cat <(sed "/${1}/,\$ d" < "$tmp") \ - <(echo -n "$2") \ - <(sed "0,/${1}/ d" < "$tmp") \ - > "$tmp_" - mv "$tmp_" "$tmp" + printf '%s' "$2" > "$tmp_" + + sed -Ei " + /${1}/ { + r $tmp_ + d + } + " "$tmp" else if [[ "$3" == rm-plus-one ]] then - sed -i "/${1}/,+1 d" "$tmp" + sed -Ei "/${1}/,+1 d" "$tmp" else - sed -i "/${1}/ d" "$tmp" + sed -Ei "/${1}/ d" "$tmp" fi fi } -# $1 line to delete: regular expression +# $1 line to delete: extended regular expression function delete_line { - sed -i "/${1}/ d" "$tmp" + sed -Ei "/${1}/ d" "$tmp" } |