On 9/5/21 07:37, Peng Yu wrote:
> I got 1 instead of 2 in the following example. How to count the last
> even when it does not end with a newline character? Thanks.
> 
> $ printf 'a\nb'|wc -l
> 1

A text file (in contrast to a binary file) must end on a newline character,
otherwise the remainder after the last '\n' in the file is not an entire line.

And that's what wc(1) effectively does (and says so in its man page):

  wc - print newline, word, and byte counts for each file
  ___________^^^^^^^_________________^^^^^^

If you'd like to treat the remainder as a line, then you have to add
a newline character at the end.

  $ printf 'a\nb\n' | wc -l
  2

Have a nice day,
Berny

Reply via email to