Remove blank lines from a file using grep
How can I remove blank lines from a file in UNIX?
grep -v "^$" filename > newfilename
The ^$ within the quotes is a regular expression: ^=beginning of line, $=end of line, with no characters between.
See additional helpful information in the comments below.









Jadu Saikia said on January 7, 2009
All ways together, :-)
$ grep -v ‘^$’ file
$ grep ‘.’ file
$ sed ‘/^$/d’ file
$ sed -n ‘/^$/!p’ file
$ awk NF file
$ awk ‘/./’ file
unstableme.blogspot.com
Luis said on March 13, 2009
awsome
Anonymous said on August 7, 2009
Have you tried the above commans…?
Anonymous said on August 19, 2009
Include blank lines that aren’t really blank (tabs, spaces, CRLF, etc)
grep -vc ‘^[[:space:]]*$’
Anonymous said on November 19, 2009
thanks
peterpan said on March 18, 2010
It’s cool~~~~ thanks!
sonu said on May 13, 2010
both are corrects with sed and with grep but i have same question as anonymous
Mark said on June 24, 2010
add
| grep -v “^$”
to the end of the command chain to eliminate blank lines.
Tntnt said on October 10, 2010
Thank you
colacao said on February 22, 2011
this is the the only one that worked for me on a file created on Windows, thanks bryansenter
Anonymous said on March 24, 2011
without the -c flag it worked for me.
Guest said on March 28, 2011
the problem with grep is it has a line limit (2048) lines. If you file has more lines than that it will stop when it gets there. I just learned a cool way to do the same thing using awk. I don’t think it has the same limitations.
awk ‘{ if (length($0) > 0){ print $0 }}’ inputfilename >> outputfilename
This will search inputfilename and as long as the line has one character on it it will be sent to the outputfilename eliminating blank lines in outputfilename.
2100822 said on April 28, 2011
thank you
Latthe said on August 8, 2011
My Name is Latthe
Latthe said on August 8, 2011
RDLC is worst tool