#!/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 "$@"