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." />

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.

 

About Quinn McHenry

Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
View more articles by Quinn McHenry

The Conversation

Follow the reactions below and share your own thoughts.

16 Responses to “Remove blank lines from a file using grep”

  1. January 07, 2009 at 11:01 am, Jadu Saikia said:

    All ways together, :-)

    $ grep -v ‘^$’ file
    $ grep ‘.’ file
    $ sed ‘/^$/d’ file
    $ sed -n ‘/^$/!p’ file
    $ awk NF file
    $ awk ‘/./’ file

    unstableme.blogspot.com

    Reply

  2. March 13, 2009 at 8:46 pm, Luis said:

    awsome

    Reply

  3. August 07, 2009 at 10:22 pm, Anonymous said:

    Have you tried the above commans…?

    Reply

  4. August 19, 2009 at 3:44 pm, Anonymous said:

    Include blank lines that aren’t really blank (tabs, spaces, CRLF, etc)

    grep -vc ‘^[[:space:]]*$’

    Reply

    • February 22, 2011 at 6:36 pm, colacao said:

      this is the the only one that worked for me on a file created on Windows, thanks bryansenter

      Reply

    • March 24, 2011 at 4:04 am, Anonymous said:

      without the -c flag it worked for me.

      Reply

  5. November 19, 2009 at 9:35 pm, Anonymous said:

    thanks

    Reply

  6. March 18, 2010 at 7:07 am, peterpan said:

    It’s cool~~~~ thanks!

    Reply

  7. May 13, 2010 at 8:28 am, sonu said:

    both are corrects with sed and with grep but i have same question as anonymous

    Reply

  8. June 24, 2010 at 3:12 pm, Mark said:

    add
    | grep -v “^$”
    to the end of the command chain to eliminate blank lines.

    Reply

  9. October 10, 2010 at 6:45 pm, Tntnt said:

    Thank you

    Reply

  10. March 28, 2011 at 10:36 pm, Guest said:

    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.

    Reply

  11. April 28, 2011 at 9:34 am, 2100822 said:

    thank you

    Reply

  12. August 08, 2011 at 1:44 pm, Latthe said:

    My Name is Latthe

    Reply

    • August 08, 2011 at 1:48 pm, Latthe said:

      RDLC is worst tool

      Reply

  13. December 11, 2012 at 10:05 am, Alexcy said:

    Thanks very much! I like the grep ‘.’ file for it’s simplicity :)

    Reply

Leave a Reply