In count (non-blank) lines-of-code in bash they explain how to count the number of non-empty lines.
But is there a way to count the number of blank lines in a file? By blank line I also mean lines that have spaces in them.
'grep -cv -P '\S' filename' will count the total number of blank lines in the file.
To match empty lines, use the pattern ' ^$ '. To match blank lines, use the pattern ' ^[[:blank:]]*$ '. To match no lines at all, use the command ' grep -f /dev/null '.
Therefore, sum(line. isspace() for line in f) returns the number of lines that are considered empty.
Use the wc command to count the number of lines, words, and bytes in the files specified by the File parameter.
Another way is:
grep -cvP '\S' file
-P '\S'
(perl regex) will match any line contains non-space-v
select non-matching lines-c
print a count of matching linesIf your grep doesn't support -P
option, please use -E '[^[:space:]]'
One way using grep
:
grep -c "^$" file
Or with whitespace:
grep -c "^\s*$" file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With