[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sort : skipping "header" row(s) - possibile new command line option?

From: Pádraig Brady
Subject: Re: sort : skipping "header" row(s) - possibile new command line option?
Date: Thu, 20 Nov 2014 12:05:05 +0000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0

On 20/11/14 10:32, Bernhard Voelker wrote:
> On 11/20/2014 07:24 AM, Bradley Dean wrote:
>> Thanks! Piping through ( sed -u 1q && sort ... ) works for me and is
>> less typing than my example workaround.
> BTW: Please note that older sed versions might slurp more input than
> needed - even with the -u option - e.g. on SLES11 the subsequent sort
> won't see any input (sed 4.1.5):
>   $ df | { sed -u 1q && sort -k5,5n; }
>   Filesystem                 1K-blocks      Used Available Use% Mounted on
> BTW2: Although "sed -u 1q" should be the ~same as "head -n1", the latter
> also consumes the whole df(1) output (even the latest git version):
>   $ src/df | { src/head -n1 && src/sort -k5,5n; }
>   Filesystem                          1K-blocks      Used Available Use% 
> Mounted on
> It's not clear for me from the specification [0] wether head(1) is
> allowed to consume more than specified by the -n option.
> Padraig?
> [0]

Yes that's a bit awkward. sed is able to do this as it consumes 1 byte at a 
head -n1 works as expected in this regard when reading from file where it
can seek back to the correct location. However as you see it breaks down
when reading from a pipe.

You could split up the data to achieve this as follows,
though that's a bit awkward and non efficient.

  df | tee >(head -n1) >(tail -n+2 | sort -k5,5n) >/dev/null

I suppose we could make `head` responsive to `stdbuf -i0`.
In that case the input buffer size is set to 1.
So we could augment `head` like:

  # include <stdio_ext.h>


  static bool
  unbuffered (FILE const * filep)
    return __fbufsize(file) == 1;
    return false;


  bufsize = unbuffered (stdin) ? 1 : BUFSIZE;

Oh actually I see fbufmode() in gnulib. It would be best to use that.
I need to check that stdbuf -i0 adjusts things accordingly for that.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]