aboutsummaryrefslogtreecommitdiff
path: root/basic.sh
diff options
context:
space:
mode:
Diffstat (limited to 'basic.sh')
-rw-r--r--basic.sh32
1 files changed, 20 insertions, 12 deletions
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"
}