Putorius
Linux Quick Tips

How To Grep Out Blank Lines In A Text File

Q: I have a large configuration file with a lot of comments and blank lines.  When I cat the file I can only see the last seven lines.  There are only about sixteen lines that are not commented out or blank.  How can I use grep (or awk/sed) to print the file to the screen without the blank lines?

A: This is very simple using grep and a regex.

For an example let’s use the following text (LINES NUMBERED FOR CLARITY):

      1 This is a file
      2 # just for testing
      3 With a comment
      4 
      5 
      6 
      7 Then some blank lines
      8 
      9 # Then some more comments
     10 
     11 And some more blank lines…
     12 
     13 
     14 
     15 Then the end of the file

The first way to do it is basically grep every "character" using the "." regex. A period in regular expressions matches any single character.

$ grep . testfile.txt 
This is a file
# just for testing
With a comment
Then some blank lines
# Then some more comments
And some more blank lines…
Then the end of the file

In the example above, we basically told grep to search for ANY character. This will NOT work if the lines are not truly blank (if they contain spaces, tabs or carriage returns).

If you want to remove the comments as well:

$ grep . testfile.txt | grep -v "^#"
This is a file
With a comment
Then some blank lines
And some more blank lines…
Then the end of the file

Here is another way using a different regex.

$ grep -v ^$ testfile.txt 
This is a file
# just for testing
With a comment
Then some blank lines
# Then some more comments
And some more blank lines…
Then the end of the file

The caret symbol ^ means the beginning of the line and the dollar sign $ means the end of the line.  So this regular expressions means match all lines that have nothing on them since ^ is the beginning of the line and $ is the end of the line and there is nothing in between them.

The -v switch tells grep to skip or don’t print the lines that match the following regex.  

We can also use this to remove the comments.  Using the ^# regex we can tell grep to skip or not print the lines starting with a #.  Remember caret ^ is the beginning of a line, so ^# means match any lines starting with a pound sign.

$ grep -v '^$|^#' testfile.txt 
This is a file
With a comment
Then some blank lines
And some more blank lines…
Then the end of the file

There is always more than one way to skin a cat.  That saying is definitely true for Linux.  

Anyone can feel free to leave a comment and tell us how you would do it.

Exit mobile version