Hone

Lessons · Terminal · wc -l counts newlines

wc counts newlines

wc -l counts newline characters, which is the same as lines except when the last line has no newline at the end.

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

It is how a count comes out one short and nothing looks wrong.

How to think about it

Know which one you are asking for: -l lines, -w words, -c bytes.

Worked example

printf 'a\nb\nc\n' | wc -l
3. Three newlines, three lines.
printf 'a\nb\nc' | wc -l
2. The same visible three lines, but the last has no newline to count.
printf '' | wc -l
0. Empty input is valid input.

Your turn

Count the lines in app.log.

wc  app.log

The trap

Files edited by hand and files written by a program differ on this more often than you would expect.

Practise wc -l counts newlines on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.