1827909 Members
2376 Online
109971 Solutions
New Discussion

sort command

 
Srinivas_9
Occasional Advisor

sort command

Sort experts please help
I am trying to come up with a sort command that will sort the following input ...first on the 1st field in ascending order then the on 2nd field in numerically descending order based on the digits that follows the string "City"

========Input===========
Akron C9.23.A1.City11
Baltimore C8.90.C1.City4
Chattanooga C51.9.A1.City5
Akron C9.99.A1.City8
Akron C9.21.A1.City9
Baltimore C8.99.C1.City99
Baltimore C8.99.C1.City1
Chattanooga C51.9.A1.City6

========Expected Output===========
Akron C9.23.A1.City11
Akron C9.21.A1.City9
Akron C9.99.A1.City8
Baltimore C8.99.C1.City99
Baltimore C8.90.C1.City4
Baltimore C8.99.C1.City1
Chattanooga C51.9.A1.City6
Chattanooga C51.9.A1.City5

Note: first the cities are sorted and then within the cities it is numerically sorted in descending order based on the digits that follow the string "City"

Tried the following command and few more combinations nothing seems to be the solution for my requirements.

sort -k1 -k2.14nr inputfile

Salome to anyone who has spend their valuable time on this
Ha Ha
10 REPLIES 10
Tiziano Contorno _
Valued Contributor

Re: sort command

Hi Srinivas, this is the most long and less efficient script I could elaborate :)

for CITY in $(sort -uk1,1 tmp_input | awk '{print $1}')
do
for NUM in $(awk -F'City' -vCITY=$CITY '$1 ~ CITY {print CITY, $2}' tmp_input | sort -n | awk '{print $2}')
do
echo "$(awk -F'City' -vNUM=$NUM -vCITY=$CITY '$1 ~ CITY && $2 ~ NUM' tmp_input)"
done
done

And this gives this:

Akron C9.23.A1.City11
Akron C9.99.A1.City8
Akron C9.21.A1.City9
Baltimore C8.99.C1.City1
Baltimore C8.90.C1.City4
Baltimore C8.99.C1.City99
Chattanooga C51.9.A1.City5
Chattanooga C51.9.A1.City6

So no perfect ordering since you have mixed one-two char numbers.

It could be a starting point.

Regards.
Peter Nikitka
Honored Contributor

Re: sort command

Hi,

first: it is always advisable to restrict the search, if multiple fields are involved.
However, this does not give your desired result as well:
sort -k1,1 -k2.14nr inputfile

I have seen several times, that the sort-command had problems when dealing with numerical mode AND dots '.' in the string to be sorted.
Try this hack (you can use another 'interim char' as § as long it is not part of your data). It works with Solaris8 (just no HP at hand).


sed -e 's/ /§/' -e 's/City/&§/' inputfile | sort -t§ -k1,1 -k3,3nr | sed -e 's/§/ /' -e 's/§//'

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"
Rodney Hills
Honored Contributor

Re: sort command

What if adjust the input file for sorting, then put it back-

sed 's/City/City /' inputfile | sort -k1 -k3nr | sed 's/City /City/'

HTH

-- Rod Hills
There be dragons...
Kent Ostby
Honored Contributor

Re: sort command

sort -k1b,1 -k2.14nr,2.16 inputfile
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
A. Clay Stephenson
Acclaimed Contributor

Re: sort command

My cut at this would be to use awk to extract the digits following city then sort it and finally reassemble it with awk.

awk '{ split($2,x,"City"); print $1, x[2], $2 }' | sort -k 1,1 -k 2nr,2nr | awk '{print $1, $NF}' < infile > outfile
If it ain't broke, I can fix that.
Srinivas_9
Occasional Advisor

Re: sort command

Guys thanks a lot for the overwhelming response. As this is a minor requirement in a very complicated script, I will let u know which of the solutions offered below suits best for me.

You guys are awesome.
Ha Ha
Sandman!
Honored Contributor

Re: sort command

Hi Srinivas,

In your command sequence the "-k1" will try to sort from the first field to the end of the line...so your next "-k2.14nr" sort key will be ignored. You need to tell sort to restrict the first key "-k1,1" before moving onto the second sort key "-k2.14nr".

# sort -k1,1 -k2.14nr inputfile

cheers!
Arturo Galbiati
Esteemed Contributor

Re: sort command

Hi Srinivas,
sort -k1b,1 -k2.14nr,2.16 inputfile
run on my HPUX1i:

10:12 GPOP09BN hpbbnn1/tmp/> cat f1
Akron C9.23.A1.City11
Baltimore C8.90.C1.City4
Chattanooga C51.9.A1.City5
Akron C9.99.A1.City8
Akron C9.21.A1.City9
Baltimore C8.99.C1.City99
Baltimore C8.99.C1.City1
Chattanooga C51.9.A1.City6

10:16 GPOP09BN hpbbnn1/tmp/> sort -k1b,1 -k2.14nr,2.16 f1
Akron C9.23.A1.City11
Akron C9.21.A1.City9
Akron C9.99.A1.City8
Baltimore C8.99.C1.City99
Baltimore C8.90.C1.City4
Baltimore C8.99.C1.City1
Chattanooga C51.9.A1.City6
Chattanooga C51.9.A1.City5

HTH,
Art
Srinivas_9
Occasional Advisor

Re: sort command

Guys thanks for the solutions provided.
Some worked some didnt.

But I picked Rodney Hills solution for its simplicity and suited my needs exactly.

LongLive HPForum.
Ha Ha
Srinivas_9
Occasional Advisor

Re: sort command

Closing thread.
Ha Ha