- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- sort command
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 03:40 AM
04-18-2006 03:40 AM
sort command
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 04:14 AM
04-18-2006 04:14 AM
Re: sort command
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 04:22 AM
04-18-2006 04:22 AM
Re: sort command
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 04:26 AM
04-18-2006 04:26 AM
Re: sort command
sed 's/City/City /' inputfile | sort -k1 -k3nr | sed 's/City /City/'
HTH
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 04:39 AM
04-18-2006 04:39 AM
Re: sort command
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 04:39 AM
04-18-2006 04:39 AM
Re: sort command
awk '{ split($2,x,"City"); print $1, x[2], $2 }' | sort -k 1,1 -k 2nr,2nr | awk '{print $1, $NF}' < infile > outfile
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 08:00 AM
04-18-2006 08:00 AM
Re: sort command
You guys are awesome.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 11:44 AM
04-18-2006 11:44 AM
Re: sort command
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2006 08:17 PM
04-18-2006 08:17 PM
Re: sort command
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2006 01:53 PM
04-19-2006 01:53 PM
Re: sort command
Some worked some didnt.
But I picked Rodney Hills solution for its simplicity and suited my needs exactly.
LongLive HPForum.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2006 01:56 PM
04-19-2006 01:56 PM