I am running the following command to count the number of rows in a CSV file:
wc -l filename.csv
Sometimes the row count is correct and other times it is 1 less than the actual count. All of the files are in the same format with the same header row. My first suspicion is the header row but it doesn't seem like the command would differentiate between the header and other rows. The files are saved with the same encoding utf-8.
Is this an issue with the formatting of the CSV files and/or a nuance of the wc command?
Assumptions:
\nat the end of the last line.csvdata fields do not include embedded linefeeds (\n); otherwise the count will return the number of lines in the file as opposed to the number of records in the fileSetup some test files:
We can see that
wc -l'misses' the last line fromfile1:One
awkapproach that works for both files:An alternative approach would have the OP go through and append a
\nto the last line of files where the\nis missing.A web search on
bash insure last line has linefeedwould be a good place to start looking for solutions ...