Operating System - HP-UX
1833210 Members
2811 Online
110051 Solutions
New Discussion

How to sort fields of unequal lenght

 
SOLVED
Go to solution
Frank de Vries
Respected Contributor

How to sort fields of unequal lenght

Hi,
I have a file with source and destination ip@ and the traffic in bytes, delimited by ;
10.214.5.205;10.218.25.220;3568
10.214.4.235;10.217.25.26;250
10.214.3.2;10.216.25.26;1002506
10.214.3.20;10.219.25.200;4569870125
10.214.2.215;10.215.25.26;4024
10.214.1.205;10.214.25.26;10250

I want to sort on the traffic field to
see the top usages

I tried
cat test.sh | awk -F ";" {' print $3";"$2$1 '} | sort -k1,1
but that gives unsatisfactory output:
1002506;10.216.25.2610.214.3.2
10250;10.214.25.2610.214.1.205
250;10.217.25.2610.214.4.235
3568;10.218.25.22010.214.5.205
4024;10.215.25.2610.214.2.215
4569870125;10.219.25.20010.214.3.20

What we need to see is this:
4569870125;10.219.25.20010.214.3.20
1002506;10.216.25.2610.214.3.2
10250;10.214.25.2610.214.1.205
4024;10.215.25.2610.214.2.215
3568;10.218.25.22010.214.5.205
250;10.217.25.2610.214.4.235

I tried in perl with sort() but
it can't handle the field mappings.

So know I have run out of ideas
Thanks for your help

Look before you leap
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor

Re: How to sort fields of unequal lenght

Hi Frank:

Add the '-n' switch to your sort:

# awk -F ";" {' print $3";"$2$1 '} /tmp/data |sort -kn1,1

Regards!

...JRF...
Peter Nikitka
Honored Contributor

Re: How to sort fields of unequal lenght

Hi,

I think it is best to add the field delimiter ';' and the numreic option:

awk -F';' '{print $3";"$2$1}' test.sh | sort -t';' -nrk1,1

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
James R. Ferguson
Acclaimed Contributor

Re: How to sort fields of unequal lenght

Hi (again) Frank:

Yes, I agree with Peter, and I missed that. Add the field delimiter to the 'sort' too.

Regards!

...JRF...
Frank de Vries
Respected Contributor

Re: How to sort fields of unequal lenght

Thanks for jogging my memory !!
Solution was so close and yet so far.

Anyone know how to do this in perl examples ??
Look before you leap
H.Merijn Brand (procura
Honored Contributor
Solution

Re: How to sort fields of unequal lenght

Trie to find the example of how to write Schwartian Transsforms

my @sorted =
map { $_->[0] }
sort { $b->[9] <=> $a->[9] ||
$a->[1] <=> $b->[1] }
map { [ $_, split m/\D+/, $_ ] }
@unsorted;

Extend the sort to your content

Enjoy, Ha ve FUN! H.Merijn
Enjoy, Have FUN! H.Merijn