22

I have to get the count of lines from incoming CSV files.

I have used the following command to get the count.

wc -l filename.csv

Consider a file coming with 1 record, I am getting some files with a \* at the start, and for those files if I issue the above command it returns count of 0.

Why is \* at the beginning of the file not register as a counted line and is there a work around?

  • try "cat filename.csv | wc -l" – chaput Mar 19 '14 at 16:30
  • 4
    "cat filename.csv | wc -l" logically does the same thing as "wc -l filename.csv" but is less efficient and elegant – Alex Mar 19 '14 at 16:47
  • Devoloper250, could you clarify that last paragraph a bit more? An example might help too. – Alex Mar 19 '14 at 16:48
  • eg: Iam getting csv files with 1 record which have *(asterisk) in the start of line. When i issue wc -l fn.csv iam getting count as 0 but ideally it should be 1. Does *(asterisk) have any specific thing to do there like other wildcards ?? – Devoloper250 Mar 20 '14 at 6:15
  • What are the results when you run "cat filename.csv"? Also, what distribution are you using? – JNevill Mar 20 '14 at 15:40
9

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.

  • the awk command works on OS X – Micah Stubbs Sep 6 '16 at 18:25
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.