mirror of https://github.com/rwxrob/dot
Add ssection
parent
7aad7ce9a3
commit
6351055d6d
@ -0,0 +1,35 @@
|
||||
#!/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"
|
||||
continue
|
||||
fi
|
||||
|
||||
[[ "$in" == no ]] && echo "$line"
|
||||
|
||||
done
|
||||
|
||||
}
|
||||
|
||||
ssection "$@"
|
Loading…
Reference in New Issue