Re: asort problem gawk (148 Views)
Reply
Regular Advisor
Chris Frangandonis
Posts: 135
Registered: ‎02-19-2002
Message 1 of 5 (148 Views)
Accepted Solution

asort problem gawk


Hi All,

I am trying to sort on field 1 9 and 10 which I am doing corretly but my output is printing the array + line which I dont wont

I only require the line


What am i doing wrong

gawk 'BEGIN{FS="|"}{
a[NR]= $1 $9 $10 " " $0
}
END{n=asort(a)
for(i=1;i<=n;++i){
print a[i]
}
}' $1

File
====
001CR|00210|00311|004000001|005655020001086961|00627813121513|00727796148609|00827813121513|00920101112|010130838|01141|0120|01341|01441|015001|016D082|017VODAC|0181|0
19107|0201105|021|022|023O|024|025T11|026352725030016860|027|028|029|030437520079|031|032|033|034|035|036|037|03827813121513|0390796148609|
001CR|00210|00311|004000002|005655020002100690|00627735523205|00727813025280|00827813025280|00920101112|010130745|01194|0120|01394|01494|015001|016D083|017MOBILE|0182|
019107|0202095|021|022|023I|024|025T11|026355806023206690|027|028|029|030437565402|031|032|033|034|035|036|037|03827735523205|03927813025280|
001CR|00210|00311|004000003|005655020001270625|00627813037563|007184|00827813037563|00920101112|010130940|011|012|013|014|015001|016D004|017TOLLFR|0185|019102|0203005|
021|022|023O|024|025T22|026351989007412940|027|028|029|030437565403|031|032|033|034|035|036|037|03827813037563|039184|

Output
======
[b]001CR00920101112010054743 [\b]001CR|00210|00311|004004717|005655020001939748|00627813007777|007internet|00827813007777|00920101112|010054743|011|012|013|014|015001|016D004
|017MOBILE|0181|019105|0201101|021|022|023O|024|025B81|026|02743786|02820132|02923654|030437782875|03141.151.254.69|032|033|034|035|036|037|03827813007777|039internet|
001CR00920101112010061158 001CR|00210|00311|004011415|005655020001519236|00627813165551|007internet|00827813165551|00920101112|010061158|011|012|013|014|015001|016D004
|017MOBILE|0181|019105|0201101|021|022|023O|024|025B81|026|02710356|0283188|0297168|030438012599|03141.151.254.69|032|033|034|035|036|037|03827813165551|039internet|
001CR00920101112010065147 001CR|00210|00311|004001202|005655020001803818|00627813713926|007internet|00827813713926|00920101112|010065147|011|012|013|014|015001|016D004
|017MOBILE|0181|019105|0201101|021|022|023O|024|025B81|026|027768263|028263492|029504771|030437535958|03141.151.254.69|032|033|034|035|036|037|03827813713926|039intern
et|

Diresired output
================
001CR|00210|00311|004004717|005655020001939748|00627813007777|007internet|00827813007777|00920101112|010054743|011|012|013|014|015001|016D004
|017MOBILE|0181|019105|0201101|021|022|023O|024|025B81|026|02743786|02820132|02923654|030437782875|03141.151.254.69|032|033|034|035|036|037|03827813007777|039internet|
001CR|00210|00311|004011415|005655020001519236|00627813165551|007internet|00827813165551|00920101112|010061158|011|012|013|014|015001|016D004
|017MOBILE|0181|019105|0201101|021|022|023O|024|025B81|026|02710356|0283188|0297168|030438012599|03141.151.254.69|032|033|034|035|036|037|03827813165551|039internet|
001CR|00210|00311|004001202|005655020001803818|00627813713926|007internet|00827813713926|00920101112|010065147|011|012|013|014|015001|016D004
|017MOBILE|0181|019105|0201101|021|022|023O|024|025B81|026|027768263|028263492|029504771|030437535958|03141.151.254.69|032|033|034|035|036|037|03827813713926|039intern
et|

Thanks
Chris
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 5 (148 Views)

Re: asort problem gawk

HI Chris:

Your posted input ("file") doesn't appear to contain some of the same strings as either output file. For example, the sequence "internet" doesn't appear in the input.

The fact that you offered input and output goes a long way to helping, but the Forums mangle text. Hence, it would be far better to attach files. Yes, there is only one attachment allowed per post, but that isn't a concern.

> but my output is printing the array + line which I dont wont

You prepend the sort keys to the record, but never snip them off (or skip over them) once the sort has finished and you are printing.

Regards!

...JRF...
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 5 (148 Views)

Re: asort problem gawk

Hi (again) Chris:

> I am trying to sort on field 1 9 and 10 which I am doing corretly but my output is printing the array + line which I dont wont

And this variation splits the sort keys from the remainder of the line:

gawk 'BEGIN{FS="|"}{
a[NR]= $1 $9 $10 " " $0
}
END{n=asort(a)
for(i=1;i<=n;++i){
s=a[i]
split(s,aa," ")
print aa[2]
}
}' $1

...Given input like:

3|x|x|x|x|x|x|x|b|b|zzz
3|x|x|x|x|x|x|x|c|b|zzz
3|x|x|x|x|x|x|x|a|b|zzz

...the output using the above script is:

3|x|x|x|x|x|x|x|a|b|zzz
3|x|x|x|x|x|x|x|b|b|zzz
3|x|x|x|x|x|x|x|c|b|zzz

Regards!

...JRF...
Please use plain text.
Regular Advisor
Chris Frangandonis
Posts: 135
Registered: ‎02-19-2002
Message 4 of 5 (148 Views)

Re: asort problem gawk

Hi James,

Thanks

It slip my mind to do the split on the 2 Fields and sorry about the inputs.

Thanks Again
Chris
Please use plain text.
Regular Advisor
Chris Frangandonis
Posts: 135
Registered: ‎02-19-2002
Message 5 of 5 (148 Views)

Re: asort problem gawk

James

have resolved my question
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation