How do I count text lines?
How do I count how many lines are in text file. e.g :
command file.txt
Note, I want to only count the non-empty lines (lines without counting white space and tabs)?.
Try sed
:
sed '/^$/d' file.txt | wc -l
If you have any lines containing only spaces or tabs and if you want to ignore them as well from count:
sed '/^[[:blank:]]*$/d' file.txt | wc -l
The above answer are correct but slightly different, you can use the grep
to for easier code such grep -vc '^$' file.txt
For example(A):file.txt
$grep -vc '^$' file.txt
1 First line #This is two tabs to comment.
2
4
3 Fourth line #Another two tabs to comment.
$2
For example(B):file.txt
$sed '/^$/d' file.txt | wc -l
1 First line #This is two tabs to comment.
2
4
3 Fourth line #Another two tabs to comment.
$4
Notice the result is 4! when we want to expect only two. but this counts the tabs in between the content and comment as well.
Notice the counts from 0 and counts from 1 its different from the grep to the sed as I remember for more details search for grep or sed.