Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use sed to delete multiple lines when the pattern is matched and stop until the first blank line?

Tags:

linux

shell

sed

I am very new to shell script.

How can I delete multiple lines when the pattern is matched and stop deleting until the first blank line is matched?

like image 812
Kintarō Avatar asked Dec 24 '22 17:12

Kintarō


1 Answers

You can do this:

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

This will select all the lines starting from STARTING_PATTERN upto a blank line ^$ and then delete those lines.

To edit files in place, use -i option.

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

Or using awk:

awk 'BEGIN{f=1} /STARTING_PATTERN/{f=0} f{print} !$0{f=1}' filename
like image 156
Rakholiya Jenish Avatar answered May 09 '23 01:05

Rakholiya Jenish