Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match empty lines in a file with 'grep'

Why does

grep -c '^\n' myfile.txt 

return 0 when there are empty lines in the file?

If there is an empty line, it starts with a new line, right?

Please correct me if I am wrong.

like image 323
Bhargav Ponnapalli Avatar asked Sep 02 '13 05:09

Bhargav Ponnapalli


People also ask

How do I search for blank lines in a file?

You can use cap (^) and Dollar ($) in grep command to display empty lines in a file.

Do not grep empty lines?

By Using [: space:]Grep's –v option will help print lines that lack blank lines and extra spacing that is also included in a paragraph form. You will see that extra lines are removed and output is in sequenced form line-wise. That's how grep –v methodology is so helpful in obtaining the required goal.


1 Answers

The regular expression to match the end of the line is $, not \n (since grep works a line at a time, it ignores the newlines between lines).

grep -c '^$' myfile.txt 
like image 190
Barmar Avatar answered Sep 28 '22 00:09

Barmar