How will you list only the empty lines in a file (using grep)?

Showing Answers 1 - 22 of 22 Answers


  • Jul 9th, 2005

grep -c "^$" filename.txt  

Pallavii Potdaar

  • Feb 15th, 2006

grep "^[      ]*$" filename.txt

In character set (between [ and ] one space and tab is given)

this command will gives all the blank line including those having space and tabs (if pressed)only

  Was this answer useful?  Yes


  • Jun 4th, 2008

We can do this efficiently through awk script

awk '{
        if (NF == 0)
          print "Here comes the empty line"
          print $0
     }' filename

NF=number of fields in the current line
$0=current line

  Was this answer useful?  Yes


  • Feb 14th, 2010

Below method tries to answer the question without using grep.

step1) sort -o < filename | tee filename | uniq -c
Now the input file-filename is sorted and output gives the number of blank lines. If b is the number of blank lines, then

step2) head -b filename

Please correct me if i am wrong

  Was this answer useful?  Yes

You can following commands:
grep '^$' ### Will just show you all the blank lines -- not much useful
grep -c '^$' ##### will give you total count of number of blank lines
grep -n '^$' #### This will show all the blank lines along with their line numbers ... useful


  • Jul 23rd, 2011

grep -c "^$" file name
here -c option count the no.of empty line..the anchors denotes the empty line from your file name

  Was this answer useful?  Yes


  • Apr 20th, 2012

grep "^$" filename

  Was this answer useful?  Yes


  • Apr 3rd, 2015

sed /^$/ !d filename
sed -n /^$/p filename

  Was this answer useful?  Yes


  • May 8th, 2015

I think it would be better to use this.
sed -n /^$/p shit.txt |wc -l

  Was this answer useful?  Yes


  • Sep 27th, 2015

grep ^$

  Was this answer useful?  Yes


  • Nov 5th, 2015

grep -c ^$ filename

  Was this answer useful?  Yes

Sumit Burnwal

  • Feb 3rd, 2016

$ grep ^$ filename

  Was this answer useful?  Yes


  • Feb 20th, 2016

$grep -c "^$" file

  Was this answer useful?  Yes


  • May 24th, 2016

$grep -n ^$ file1.txt

  Was this answer useful?  Yes

mallikarjuna reddy

  • Jul 23rd, 2019

grep -n ^$ file1.txt this command will give the all empty lines numbers display
grep -n ^$ file1.txt | wc -l this command will give the all empty lines number counts in file

  Was this answer useful?  Yes

Sathish Krishnan Venkatachalam

  • Aug 7th, 2020

grep "^$" file.txt

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.


Related Answered Questions


Related Open Questions