PDA

View Full Version : grep ALLOW empty lines



mahy
April 6th, 2011, 03:42 PM
Hello,

I've got an unusual problem, I need to filter out matching lines of a file, but ALLOWING empty lines. Grep is behaving a little strange


cat file | grep '^$'
This works and of course ONLY prints empty lines. That's not what I want.


cat file | grep '^$ \| PATTERN'
This DOES NOT work and ONLY prints lines matching the "PATTERN", ignoring empty lines. Again, not what I want. I want BOTH the matching lines AND empty lines.

Many thanks in advance for any help!

gmargo
April 6th, 2011, 03:52 PM
Use grep(1) with -E option, or just use egrep(1).


cat file | egrep '^$|PATTERN'

Vaphell
April 6th, 2011, 03:53 PM
echo $'abc PATTERN def\nsomething\n\nabc PATTERN def' | grep -E '^$|PATTERN'

result:
abc PATTERN def

abc PATTERN def

btw, drop cat, grep can read the file on its own just fine

grep <stuff> file

mahy
April 7th, 2011, 08:55 AM
Thx a lot, egrep worked! :guitar: