0

I have a text file. Some lines contain text while others do not (just empty line without any spaces or characters:

123

// comment
45

As you can see, there is a line above //comment. If I want to count the times such line exist in my file, how can I grep them?

2

You can grep empty lines with grep '^$' file and count the lines with wc -l. Combined it's:

grep '^$' file | wc -l

or even shorter with grep's count option -c or --count

grep -c '^$' file
  • 2
    See also grep -c – Stephen Harris Sep 8 at 22:33
  • Thanks @StephenHarris ! I totally forgot this option. – Freddy Sep 8 at 22:37
  • 1
    grep -cv . file where we reverse the match. – Rakesh Sharma Sep 9 at 4:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.