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: Rasmus Borup Hansen
Subject: Re: sort : skipping "header" row(s) - possibile new command line option?
Date: Wed, 19 Nov 2014 10:01:29 +0100

I'd second that such an option would be useful. It appears that "-H" is free!

I realised that I've been doing the head and tail construct too many times so I 
made a small shell script (I call it hsort) that works like sort, but printing 
the first line right away, and only reads the data once (so it works with 
pipes):

#!/bin/bash

# First try to parse options in the same way as sort, storing the options in an 
array. Note that the option --files0-from=F is not supported.
eval set -- "`getopt -o CMRVbcdfghik:mno:rsS:t:uz -l 
batch-size:,buffer-size:,check:,compress-program:,debug,field-separator:,help,parallel:,random-source:,sort:,unique,version,zero-terminated
 -- "$@"`"
declare -a SORT_OPTS
while [ "$1" != "--" ] ; do
    SORT_OPTS+=("$1")
    shift
done
shift

if [ "$#" -gt 0 ]; then
    exec < <(cat -- "$@")
fi

set -- "${SORT_OPTS[@]}"
read line
printf "$%s\n" "$line"
sort "$@"

It even works with -t $'\t' that is used for sorting tab delimited files. I 
might have missed an option to sort, and I don't know if getopt works exactly 
like the option parsing in sort.

Best,

Rasmus Borup Hansen

Intomics is a contract research organization specialized in deriving core 
biological insight from large scale data. We help our clients in the 
pharmaceutical industry develop tomorrow's medicines better, faster, and 
cheaper through optimized use of biomedical data.
-----------------------------------------------------------------
Hansen, Rasmus Borup              Intomics - from data to biology
System Administrator              Diplomvej 377
Scientific Programmer             DK-2800 Kgs. Lyngby
                                  Denmark
E: address@hidden               W: http://www.intomics.com/
P: +45 5167 7972                  P: +45 8880 7979

> On 19 Nov 2014, at 05:21, Bradley Dean <address@hidden> wrote:
> 
> Greetings,
> 
> this is a question about a possible new command-line option for sort
> that I'd be interested in implementing, but I thought I'd throw the idea
> out there first before spending time on an idea that was not going to be
> considered useful. I can't think of how I'd do this other than the way
> I've put together below as an example of an existing workaround.
> 
> When using sort to look at space-delimited data (ie with human-readable
> columns) with a header row (or rows) it's useful to be able to keep the
> header rows at the top/bottom of the data rather than having them end up
> mixed with the data after sorting.
> 
> 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.
> 
> Cheerio,
> 
> Brad
> 
> -- 
> Bradley Dean
> Email: address@hidden Skype: address@hidden
> Mobile(Aus): +61-413014395 WWW: http://bjdean.id.au/
> 



reply via email to

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