Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed: remove matching line and everything after

Tags:

sed

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?

like image 717
Anova Avatar asked Oct 16 '25 03:10

Anova


1 Answers

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.

like image 107
oguz ismail Avatar answered Oct 19 '25 08:10

oguz ismail