From 564a3e2ec83e3883cafe9efd2c6f88144a0835db Mon Sep 17 00:00:00 2001 From: Einhard Leichtfuß Date: Sun, 23 Dec 2018 05:58:13 +0100 Subject: Simplify basic.sh:replace_line and switch to EREs EREs: extended regular expressions in contrast to basic regular expressions (BREs). --- basic.sh | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'basic.sh') diff --git a/basic.sh b/basic.sh index ed4a442..7740978 100644 --- a/basic.sh +++ b/basic.sh @@ -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" } -- cgit v1.2.3