Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell: simple way to get all lines before first blank line

Tags:

shell

awk

What's the best shell command to output the lines of a file until you encounter the first blank line? For example:

output these
lines

but do not output anything after the above blank line
(or the blank line itself)

awk? something else?

like image 328
Peter Avatar asked Oct 21 '09 20:10

Peter


1 Answers

With sed:

sed '/^$/Q' <file>

Edit: sed is way, way, way faster. See ephemient's answer for the fastest version.

To do this in awk, you could use:

awk '{if ($0 == "") exit; else print}' <file>

Note that I intentionally wrote this to avoid using regular expressions. I don't know what awk's internal optimizations are like, but I suspect direct string comparison would be faster.

like image 127
Cascabel Avatar answered Nov 16 '22 01:11

Cascabel