sed

Branching Operation

Introduction#

The branching operation of sed can help control the flow of the program.

Do multiple line regexp replacing with unconditional branch

Assume that I have a file named in.txt:

$ cat in.txt
a
b
a
c
a
d

I only want to replace the a\nc with deleted, but not a\nb or a\nd.

$ sed -e ':loop         # create a branch/label named `loop`
 $!{
 N                      # append the next line of input into the pattern space
 /\n$/!b loop           # If it is not the last line go to the `loop` branch again
 }
 s/a\nc/"deleted"/' in.txt    # do replacing in the pattern space

a
b
"deleted"    # see! succeed
a
d

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow