Unix command to get number of lines in a CSV file

A trick to ensure that also non-terminated lines are counted may be:

cat filename.csv | xargs -l echo | wc -l

This seems to count all non-empty lines, but skips empty lines.

Please note that is is rather ineffective, but that is probably not a problem for occational use.

Another possibility, counts all lines including non-terminated last line:

awk '{n+=1} END {print n}' filename.csv

Tested on RHEL 6.2. YMMV.


wc will report 0 for files with only one line and no trailing newline. Maybe your one-record csv files are like this? You can look for trailing newlines with hexdump, e.g.:

hexdump -C fn.csv

Look for ascii code 0a at the end.