[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to count the last line when it does not end with a newline chara
From: |
Bernhard Voelker |
Subject: |
Re: How to count the last line when it does not end with a newline character? |
Date: |
Tue, 7 Sep 2021 12:34:56 +0200 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.0.1 |
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