Lessons · Terminal · counting lines and words
Counting lines
wc -l counts lines, wc -w words, wc -c bytes. It reads a file or whatever is piped into it.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
How many errors, how many records, how many files matched: counting is the answer to half the questions a pipeline gets asked.
How to think about it
Put wc -l at the end of a pipe to count what the pipe produced.
Worked example
printf 'ada\ngrace\nlin\n' > names.txtThree names.
wc -l < names.txt3 lines.
wc -w < names.txt3 words.
Your turn
Count the lines in orders.csv.
wc orders.csv
Try it at a real prompt
The trap
wc -l counts newline characters, so a file whose last line has no newline counts one short.
Practise counting lines and words on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.