4.20. How do I change only one section of a file?
You can match a range of lines by line number, by regexes (say, all
lines between the words "from" and "until"), or by a combination of
the two. For multiple substitutions on the same range, put the
command(s) between braces {...}. For example:
# replace only between lines 1 and 20
1,20 s/Johnson/White/g
# replace everywhere EXCEPT between lines 1 and 20
1,20 !s/Johnson/White/g
# replace only between words "from" and "until". Note the
# use of \<....\> as word boundary markers in GNU sed.
/from/,/until/ { s/\<red\>/magenta/g; s/\<blue\>/cyan/g; }
# replace only from the words "ENDNOTES:" to the end of file
/ENDNOTES:/,$ { s/Schaff/Herzog/g; s/Kraft/Ebbing/g; }
For technical details on using address ranges, see section 3.3
("Addressing and Address ranges").