Anova
Anova

Reputation: 51

sed: remove matching line and everything after

I'm trying to remove all lines after the first blank line in a file with a git filter using sed.

This seems to remove everything after the blank line

sed -i '/^$/q' test.rpt

How do I also include the blank line itself to be deleted?

Upvotes: 4

Views: 411

Answers (2)

Ravi Saroch
Ravi Saroch

Reputation: 960

Try this:-

sed -i '/^$/,$ d' inputfile

Upvotes: 2

oguz ismail
oguz ismail

Reputation: 50750

If this is GNU sed, just use Q instead of q.

sed -i '/^$/Q' test.rpt

For BSD sed, use -n switch to suppress automatic printing, and print lines manually. E.g:

sed -n -i '/^$/q;p' test.rpt

PS: You might want to change the regex to ^[[:blank:]]*$ to regard lines of all blank characters as blank lines as well.

Upvotes: 4

Related Questions