Re: sorting files with headers

From: Slowdude (slowdude_at_zerofluid.com)
Date: 01/13/05


Date: Thu, 13 Jan 2005 00:06:17 GMT


> This approach would be more efficient:

You should test for efficiency before making that claim. The numbers went
back and forth, but they are nearly the same.

trim.sh is mine
trimmorton is yours

root@zerofluid:/var/adm# wc -l messages
7293 messages
root@zerofluid:/var/adm# time /home/slowdude/bin/trim.sh messages 3 4 > sortfile

real 0m0.082s
user 0m0.020s
sys 0m0.050s
root@zerofluid:/var/adm# time /home/slowdude/bin/trimmorton.sh messages 3 4 > sortfile

real 0m0.092s
user 0m0.070s
sys 0m0.010s

It is a wash and my version is easier to read.

>
> len=$(wc -l < "$1")
> awk -vhd="$2" -vtl="$((len + 1 - $3))" 'NR>hd && NR<tl' "$1"
>
> If you don't like or don't have access to "wc", then there's various
> ways to do it just using awk, but they all require parsing the input
> file twice.
>
> Regards,
>
> Ed.



Relevant Pages

  • Re: Efficiency [Was: Re: rpc.statd attack]
    ... >> ...works just as well, or even better, with less false alarms and more ... > sys 0m0.07s ... But even without the efficiency argument, ... .siht ekil ti gnidaer eb d'uoy,werbeH ni erew ecnetnes siht fI ...
    (FreeBSD-Security)
  • Re: Less greedy pattern match
    ... reasons). ... real 0m0.859s user 0m0.749s sys 0m0.124s ... Like Janis says, though, efficiency isn't the main reason to structure your awk ...
    (comp.lang.awk)