2014-01-07

How many lines in a file?

It's an easy (but not clear, please refer to the Note of wc below) question, but sill quite funny.

Tested under the Bash shell on Ubuntu 12.04 32-bit.

(1) wc

cat sample.txt | wc -l

or

wc sample.txt | awk '{print $1}'

awk is used to extract the 1st field.

Note: wc only counts the newline characters! The last line without newline is not counted.  For example:

echo -ne "ab\ncd" | wc -l

would generate 1 (line), not 2.