--- Begin Message ---
Subject: |
`head --lines=-0' prints nothing if no newlin e at the EOF |
Date: |
Fri, 03 Jan 2014 19:50:56 +0400 |
Hi,
If one runs `head --lines=-0 somefile', he'll sometimes get no output instead
of full file contents as
expected after reading the manual:
-n, --lines=[-]K
print the first K lines instead of the first 10; with the lead‐
ing `-', print all but the last K lines of each file
It depends on whether the file has a trailing newline.
For example:
$ printf '1\n2\n3' > test1
$ printf '4\n5\n' > test2
$ head -n -0 test*
==> test1 <==
==> test2 <==
4
5
$ tail -n +0 test* # Just for comparison
==> test1 <==
1
2
3
==> test2 <==
4
5
$
I'm using Debian GNU/Linux 7 wheezy and coreutils-8.13; I've also tested
coreutils-8.22, and it has the
same issue.
A proposed patch fixing the bug is attached. It makes head behave exactly like
`tail -n +0' on same files.
--
Алексей Шилин
head_nlines_fix.patch
Description: Source code patch
--- End Message ---
--- Begin Message ---
Subject: |
Re: bug#16329: Acknowledgement (`head --lines=-0' prints nothing if no newline at the EOF) |
Date: |
Wed, 29 Jan 2014 11:55:53 +0000 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130110 Thunderbird/17.0.2 |
On 01/03/2014 08:40 PM, Алексей Шилин wrote:
> Looks like I was overly cautious about decrementing an unsigned...
>
> size_t n = bytes_read;
> while (n)
> {
> if (all_lines)
> n -= n ? 1 : 0; // ...here.
> else
>
> As it is under `while (n)' statement, n is always true here, and thus the
> ternary operator, though makes no
> harm, is needless, and the whole line can be replaced with just `n--;'. Sorry
> for that.
>
> The fixed version of the original patch is attached.
>
I've updated the patch to also handle the pipe case,
and I added a test. Will push soon.
thanks,
Pádraig.
head--lines-0.patch
Description: Text Data
--- End Message ---