- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- sort problem
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
07-13-2004 09:52 PM
07-13-2004 09:52 PM
1 aaa 7
2 bbb 3
1 aaa 4
1 aaa 5
When "sort a > b", the b would show
1 aaa 4
1 aaa 5
1 aaa 7
2 bbb 3
But I want to get the result like below:
1 aaa 7
1 aaa 4
1 aaa 5
2 bbb 3
Because it is only search column1, and "1 aaa 7" is in the first line, but not "1 aaa 4" . Does sort command can do that ? Thanks.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2004 10:09 PM
07-13-2004 10:09 PM
Re: sort problem
not what you show because that is not sorted but read the man
sort +2 -r /tmp/test.txt
1 aaa 7
1 aaa 5
1 aaa 4
2 bbb 3
Steve Steel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2004 10:20 PM
07-13-2004 10:20 PM
Re: sort problem
1 aaa 7
1 aaa 4
1 aaa 5
2 bbb 3
Becuase I select column 1 as the sort key.
See the source file,
1 aaa 7
2 bbb 3
1 aaa 4
1 aaa 5
Since
1 aaa 7 is the first line
1 aaa 4 is the third line
1 aaa 5 is the last line
So, I want sort as below results:
1 aaa 7
1 aaa 4
1 aaa 5
2 bbb 3
But sort will get the below results:
1 aaa 4
1 aaa 5
1 aaa 7
2 bbb 3
It looks line it also sort the column 2 and column3. From my point, I just want sort column1.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2004 10:36 PM
07-13-2004 10:36 PM
Re: sort problem
1 aaa 7
2 bbb 3
1 aaa 4
1 aaa 5
The output.
# more file1 | sort +1
1 aaa 4
1 aaa 5
1 aaa 7
2 bbb 3
This what i get but not 745 in the last col.
Another way to add to it is:
# more file1 | sort +1 | sort +3 -r
1 aaa 4
1 aaa 5
1 aaa 7
2 bbb 3
This is what i get but again no 745 in the last col.
That's all.
Regards,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2004 10:39 PM
07-13-2004 10:39 PM
Re: sort problem
---
Lines that otherwise compare equal are ordered with all bytes significant. If all the specified keys compare equal, the entire record is used as the final key.
---
Best regards...
Dietmar.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2004 10:46 PM
07-13-2004 10:46 PM
Re: sort problem
sort will try to check on key value first, If it is containing same vaules,it will go to next and so on.
1 aaa 7
2 bbb 3
1 aaa 4
1 aaa 5
cat test.file | sort -k 1
There is no unique field in first key,so it is coming to second. Situation at 2 column is same. At last it is coming to 3rd one.
Result will be as like that.
1 aaa 7
2 bbb 3
1 aaa 4
1 aaa 3
It will give as,
1 aaa 3
1 aaa 4
1 aaa 7
2 bbb 3
It is sorting every column if there is no unique field to validate this,
Change the input file as,
4 2 bbb 3
5 1 aaa 4
3 1 aaa 5
1 1 aaa 7
cat test.file | sort -k 1
1 1 aaa 7
3 1 aaa 5
4 2 bbb 3
5 1 aaa 4
It is sorting only on first field only.
Regards,
Muthukumar.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2004 10:50 PM
07-13-2004 10:50 PM
Re: sort problem
awk '{print NR, $0}' | sort -k 2,2 -k 1n,1n | cut -d ' ' -f 2-
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2004 12:19 AM
07-14-2004 12:19 AM
Re: sort problem
tant # more a
1 aaa 7
2 bbb 3
1 aaa 4
1 aaa 5
tant # head -n1 a>c && cat a|grep -v "`head -n1 a`"|sort>>c
tant # more c
1 aaa 7
1 aaa 4
1 aaa 5
2 bbb 3
All the best
Victor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2004 12:28 AM
07-14-2004 12:28 AM
Re: sort problem
Gives, what you want
Anil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2004 12:32 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2004 12:34 AM
07-14-2004 12:34 AM
Re: sort problem
From your profile;
http://forums1.itrc.hp.com/service/forums/publicProfile.do?userId=CA875590&forumId=1
You have assigned points to 426 of 1042 responses to your questions.
That relates to a lot of assistance that you've received that you've not acknowledged.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2004 01:02 AM
07-14-2004 01:02 AM
Re: sort problem
You want to take a file like this
1 aaa 7
2 bbb 3
1 aaa 4
1 aaa 5
and put it into the form of this:
1 aaa 7
1 aaa 4
1 aaa 5
2 bbb 3
Sorry, but that's not called sorting!
It appears you are trying to move the records down based upon the first value. Thus I suggest you use something like awk:
cat test.txt | awk 'BEGIN {system("rm newcraze.txt")} {print $0 > "craze"$1".tmp
";files[$1]="Y"} END {for(var=0;var<9;var++) {if (files[var] == "Y") {system("ca
t craze"var".tmp >>newcraze.txt");system("rm craze"var".tmp")}}}'
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2004 01:40 AM
07-14-2004 01:40 AM
Re: sort problem
Actually... on some systems this is called sorting, specifically 'stable' sorting.
IMHO, if one specifies explicit keys then a sort tool has no business to add in gratuitous extra keys (the whole record if equal). It could (IMHO) return records in random orders, or... when so requested in a STABLE order based on the input file order.
OpenVMS SORT (and SYNCSORT?) work that way. (See below).
Still, that does not matter because hpux sort is documented NOT to work that way.
So here is a 'one-liner' workaround...
awk '{printf ("%06d %s\n",NR,$0)}' x | sort -k 2,2 -k 3,3 -k 1,1 xx | cut -b 7-
hth,
Hein.
OpenVMS example...
$ typ tmp.tmp
1 aaa 7
2 bbb 3
1 aaa 4
1 aaa 5
$ sort/key=(pos:1,siz:5)/stable tmp.tmp sys$output:
1 aaa 7
1 aaa 4
1 aaa 5
2 bbb 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2004 01:50 AM
07-14-2004 01:50 AM
Re: sort problem
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2004 01:58 AM
07-14-2004 01:58 AM
Re: sort problem
sort -k1,2 -k3r testfile
HTH
-- Rod Hills