#!/bin/bash # # Copyright 2018 Einhard Leichtfuß # # All methods operate on the file "$tmp". typeset -a langs langs=( fra-deu deu-fra deu-eng eng-deu fra-eng eng-fra spa-eng eng-spa ) typeset -a vcs_langs vcs_langs=( deu-eng eng-deu fra-eng eng-fra spa-eng eng-spa ) function init { tmp_d="$(mktemp -d)" tmp="$tmp_d/a" tmp_="$tmp_d/b" } function cleanup { rm -rf "$tmp_d" } trap cleanup EXIT # $1: string to replace: extended regular expression # $2: string to replace with: sed replacement string function replace { sed -Ei "s/${1}/${2}/g" "$tmp" } # 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 printf '%s' "$2" > "$tmp_" sed -Ei " /${1}/ { r $tmp_ d } " "$tmp" else if [[ "$3" == rm-plus-one ]] then sed -Ei "/${1}/,+1 d" "$tmp" else sed -Ei "/${1}/ d" "$tmp" fi fi } # $1 line to delete: extended regular expression function delete_line { sed -Ei "/${1}/ d" "$tmp" }