coreutils
[Top][All Lists]
Advanced

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

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


From: Davide Brini
Subject: Re: sort : skipping "header" row(s) - possibile new command line option?
Date: Wed, 19 Nov 2014 10:04:58 +0100

On Wed, 19 Nov 2014 15:21:35 +1100, Bradley Dean <address@hidden>
wrote:

> It's often possible to do this by looking at the data twice (once to
> grab the header, once to sort) - either by dumping to a file or by
> running the generating command twice.
> 
> For example - take some_command which outputs space-delimeted data with
> 1 header row and a fourth numeric column I'd like to sort on:
> 
>   some_command > templog; head -1 templog; tail -n +2 templog | sort -n
>   -k 4
> 
> I think it would frequently be handy to be able to just ask sort to keep
> the header rows aside - especially when doing things like running the
> sort inside a "watch" loop where you'd like to be able to come back to
> it over time and see what each column is.

I'm not against having an option, but in cases like this you can do

some_command | { IFS= read -r header; echo "$header"; sort -n -k4; }

can be generalized to N header lines with a read loop, eg for 3 lines:

some_command | { n=0; while [ $n -lt 3 ]; do IFS= read -r header; echo
"$header"; ((n++)); done; sort -n -k4; }

-- 
D.



reply via email to

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