You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
rwxrob-dot/scripts/ssection

36 lines
708 B
Bash

#!/usr/bin/env bash
# This script replaces a section of text in a file marked by the bash
# regular expression passed as the first argument and ending with the
# second argument with the text in the the third argument The beginning
# and ending lines themselves are discarded when replaced.
ssection () {
local beg="$1" end="$2" buf="$3"
[[ -z "$beg" || -z "$end" ]] && echo "usage: ${0##*/} BEGX ENDX [STRING]" && return 1
local in=no
while IFS= read -r line; do
if [[ "$line" =~ $beg ]]; then
in=yes
continue
fi
if [[ "$line" =~ $end ]]; then
in=no
printf "$buf\n\n"
continue
fi
[[ "$in" == no ]] && echo "$line"
done
}
ssection "$@"