How can I use sed to delete multiple lines when the pattern matches and stops before the first blank line?

I am very new to shell script.

How do I delete multiple lines when a pattern matches, and stop deleting until the first empty line is matched?

+3


source to share


1 answer


You can do it:

sed '/STARTING_PATTERN/,/^$/d' filename

      

This will select all lines starting with STARTING_PATTERN

, up to an empty line ^$

, and then remove those lines.

To edit files in place, use the option -i

.



sed -i '/STARTING_PATTER/,/^$/d' filename

      

Or using awk

:

awk 'BEGIN{f=1} /STARTING_PATTERN/{f=0} f{print} !$0{f=1}' filename

      

+7


source







All Articles