Using sed, how do I search for a line ending with foo and then edit the next line if it starts with #bar?
Or put another way, I want to remove a comment # from the next line if it starts with #bar and the previous line ends in foo.
For example:
This is a line ending in foo
#bar is commented out
There are many lines ending in foo
#bar commented out again
I tried:
sed -i 's/^foo\n#bar/foo\nbar/' infile
P;Dfor one line only solution? – cuonglm May 24 '16 at 15:09P;Dapproaches are already covered in other answers. This answer shows the direct translation of the requirement approach (search forfoo$, get the next line, substitute) along with its limitations. – Stéphane Chazelas May 24 '16 at 15:17sed ':1;/foo$/{n;s/^#bar/bar/;b1}'– alx - recommends codidact Feb 07 '20 at 13:56:1orb1because in manysedimplementations including the original UNIX implementation, that anything would be taken as part the branching label's name (that even used to be required by POSIX). – Stéphane Chazelas Feb 07 '20 at 14:02