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?

3

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
| improve this answer | |
  • 2
    See also grep -c – Stephen Harris Sep 8 '19 at 22:33
  • Thanks @StephenHarris ! I totally forgot this option. – Freddy Sep 8 '19 at 22:37
  • 1
    grep -cv . file where we reverse the match. – Rakesh Sharma Sep 9 '19 at 4:10

Your Answer

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