1.

How Do I Change Only One Section Of A File?

Answer»

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//magenta/g; s//cyan/g; }

# replace only from the words "ENDNOTES:" to the end of FILE
/ENDNOTES:/,$ { s/Schaff/Herzog/g; s/Kraft/Ebbing/g; }

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//magenta/g; s//cyan/g; }

# replace only from the words "ENDNOTES:" to the end of file
/ENDNOTES:/,$ { s/Schaff/Herzog/g; s/Kraft/Ebbing/g; }



Discussion

No Comment Found