An interesting sort problem

Ian Malone ibmalone at gmail.com
Fri Nov 13 18:32:50 UTC 2015


On 13 November 2015 at 16:54, jd1008 <jd1008 at gmail.com> wrote:
>
>
> On 11/12/2015 07:38 PM, Michael Hennebry wrote:
>>
>> Awk can do what you want:
>> {
>>     lines[NR]=$NF " " $0
>> }
>>
>> END {
>>     PROCINFO["sorted_in"]="@val_type_asc"
>>     for line in lines {
>>         j=index(line, " ")
>>         print substr(line, j+1)
>> }
>>
> Sorry, but, since this is being archived,
> could you please dot the i's and cross the t's ?
>
> I assume $0 is the name of the file ??
> Where is BEGIN?
>
> Why are the single quote marks missing?
>
> As you script stands, it is full of syntax errors.
>

No missing quotes. It's an awk program, not a bash one, $0 is a bash
variable. The only syntax errors I see are no brackets around the for
statement and missing closing brace. My awk here is too old to support
PROCINFO["sorted_in"], so here's a slight tweak using asort:

awktest:
----
{
    lines[NR]=$NF " " $0
}

END {
    #PROCINFO["sorted_in"]="@val_type_asc"
    asort(lines, ordered)
    for (ind = 1 ; ind<=length(ordered) ; ind++) {
       line=ordered[ind]
       j=index(line, " ")
       print substr(line, j+1)
    }
}
----

awktestinput:
----
Friday Lemon abc xyz
Saturday cucumber cool
Sunday orange citrussy
Monday apple computer
----

$ awk -f awktest  < awktestinput
Sunday orange citrussy
Monday apple computer
Saturday cucumber cool
Friday Lemon abc xyz


-- 
imalone
http://ibmalone.blogspot.co.uk


More information about the users mailing list