bug-gawk
[Top][All Lists]

Re: [bug-gawk] Help ourself

 From: Andrew J. Schorr Subject: Re: [bug-gawk] Help ourself Date: Tue, 23 Apr 2019 18:48:35 -0400 User-agent: Mutt/1.5.21 (2010-09-15)

```For fully customized sorting, you must call asort with a user-supplied
comparison function:

{
data[NR]=\$0;
}

function cmpfunc(i1, v1, i2, v2,   n, f, m, g, i, c, d) {
n = split(v1, f)
m = split(v2, g)
c = ((n <= m) ? n : m)
for (i = 1; i <= c; i++) {
if ((d = f[i]-g[i]) != 0)
return (d < 0) ? -1 : 1
}
if (n != m)
return (n < m) ? -1 : 1
return 0
}

END {
n = asort(data, out, "cmpfunc")
for (i = 1; i <= n; i++)
print out[i]
}

This sorts by comparing each field numerically.

36829.00 37145.00 10801
36829.00 37145.00 12961
36829.00 37145.00 17281
36829.00 37145.00 21601
36829.00 37145.00 2161
36829.00 37145.00 4321
36829.00 37145.00 4321
36829.00 37145.00 6481
36829.00 37145.00 8641
36829.00 37145.00 8641

The output is:
36829.00 37145.00 2161
36829.00 37145.00 4321
36829.00 37145.00 4321
36829.00 37145.00 6481
36829.00 37145.00 8641
36829.00 37145.00 8641
36829.00 37145.00 10801
36829.00 37145.00 12961
36829.00 37145.00 17281
36829.00 37145.00 21601

Regards,
Andy

On Tue, Apr 23, 2019 at 09:21:43PM +0000, Tom Gray wrote:
> Here's how I sort based on a specific field:
>
>   {data[NR]=\$0;
>    sort_val[NR]=\$1;  # pick any field for the numeric sort
>   }
>
> END {
>    PROCINFO["sorted_in"]="@val_num_asc";
>    for(i in sort_val) print data[i];
> }
>
> -----Original Message-----
> From: bug-gawk <address@hidden> On Behalf Of david kerns
> Sent: Tuesday, April 23, 2019 1:31 PM
> Subject: Re: [bug-gawk] Help ourself
>
> [EXTERNAL]
>
> oops, re-send w/ mail-list
> probably the fastest/easiest is to use sort:
> sort -n -k 1 f | awk ...
>
> On Tue, Apr 23, 2019 at 11:56 AM Budi <address@hidden> wrote:
>
> > How poor I'm.. my need is the opposite,
> >
> > is there a way to force such string lines, the 1st field  to be
> > treated as number so can get sorted numerically only this field then
> > stop doing it for the rest of characters at that line
> >
> >
> > On 4/24/19, david kerns <address@hidden> wrote:
> > > awk is not strongly typed ... variables are what you coerce them into
> > > being.
> > > In the first example you assigned "a" pure numeric values.
> > > In the second example you assigned "a" all strings (each value contains 2
> > > spaces)
> > > change your first example to this:
> > >
> > > awk '{a[NR]=\$0 ""}END{ asort(a);for(;i++<NR;){ print a[i] }}' f
> > >
> > > and it will string sort too
> > >
> > > On Tue, Apr 23, 2019 at 9:41 AM Budi <address@hidden> wrote:
> > >
> > >> We have facts that
> > >> \$ export LC_ALL=C
> > >>
> > >> \$ cat f
> > >> 2161
> > >> 4321
> > >> 6481
> > >> 8641
> > >> 10801
> > >> 4321
> > >> 8641
> > >> 12961
> > >> 17281
> > >> 21601
> > >>
> > >> \$ awk '{a[NR]=\$0}END{ asort(a);for(;i++<NR;){ print a[i] }}' f
> > >> 2161
> > >> 4321
> > >> 4321
> > >> 6481
> > >> 8641
> > >> 8641
> > >> 10801
> > >> 12961
> > >> 17281
> > >> 21601
> > >>
> > >> One would accept as it's a number sort
> > >>
> > >> \$ cat f
> > >> 36829.00 37145.00 2161
> > >> 36829.00 37145.00 4321
> > >> 36829.00 37145.00 6481
> > >> 36829.00 37145.00 8641
> > >> 36829.00 37145.00 10801
> > >> 36829.00 37145.00 4321
> > >> 36829.00 37145.00 8641
> > >> 36829.00 37145.00 12961
> > >> 36829.00 37145.00 17281
> > >> 36829.00 37145.00 21601
> > >>
> > >>
> > >> \$ awk '{a[NR]=\$0}END{ asort(a);for(;i++<NR;){ print a[i]}}' f
> > >> 36829.00 37145.00 10801
> > >> 36829.00 37145.00 12961
> > >> 36829.00 37145.00 17281
> > >> 36829.00 37145.00 21601
> > >> 36829.00 37145.00 2161
> > >> 36829.00 37145.00 4321
> > >> 36829.00 37145.00 4321
> > >> 36829.00 37145.00 6481
> > >> 36829.00 37145.00 8641
> > >> 36829.00 37145.00 8641
> > >>
> > >> One would accept as it's a string sort
> > >>
> > >> How to resolve this confusing processes ?
> > >>
> > >>
> > >
> >

--