HPE GreenLake Administration
- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Sorting problem
Operating System - HP-UX
1833792
Members
2530
Online
110063
Solutions
Forums
Categories
Company
Local Language
back
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
back
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
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Topic Options
- 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-03-2001 10:03 PM
07-03-2001 10:03 PM
Sorting problem
Dear all
I would like to ask a question on 'sort' in Unix.
I have a file which contains the following records:
18,0001,abc,xyz
19,0001,wxy,ghi
I would like to sort by the second field in the record. If more than one records are found with the same second field, then I only want the first record. That is to say, in the example above, I only want to return
18,0001,abc,xyz
Another question,
How do I sort using more than one fields in the record, for example
20,123,abc,def
21,246,xyz,ijk
I would like to sort by the second and last field. How do I do do that using the 'sort' command?
Many thanks
I would like to ask a question on 'sort' in Unix.
I have a file which contains the following records:
18,0001,abc,xyz
19,0001,wxy,ghi
I would like to sort by the second field in the record. If more than one records are found with the same second field, then I only want the first record. That is to say, in the example above, I only want to return
18,0001,abc,xyz
Another question,
How do I sort using more than one fields in the record, for example
20,123,abc,def
21,246,xyz,ijk
I would like to sort by the second and last field. How do I do do that using the 'sort' command?
Many thanks
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2001 10:17 PM
07-03-2001 10:17 PM
Re: Sorting problem
Hi,
Try
sort -u -t, -k2,2 file_to_be_sorted
Rgds
Try
sort -u -t, -k2,2 file_to_be_sorted
Rgds
Permanent training makes master
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2001 10:31 PM
07-03-2001 10:31 PM
Re: Sorting problem
Hi,
This script will sort out your 'filename' in terms of 2nd field and keep it in newfile. It will keep only the first line of similar content as you required....
cat filename | awk'FS = "," {print $2}' | sort -n > tempfile
for i in 'tmpfile'
do
grep $i filename | head -1 >> newfile
done
Regarding sort in 2 fields, you have to write similar kind of script......
Hope it helps.....
This script will sort out your 'filename' in terms of 2nd field and keep it in newfile. It will keep only the first line of similar content as you required....
cat filename | awk'FS = "," {print $2}' | sort -n > tempfile
for i in 'tmpfile'
do
grep $i filename | head -1 >> newfile
done
Regarding sort in 2 fields, you have to write similar kind of script......
Hope it helps.....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2001 10:36 PM
07-03-2001 10:36 PM
Re: Sorting problem
Uhmm,
let's give this a try:
cat your_file | sort -t, -n +1 -2 | awk -F, 'BEGIN {prev="" } {if (prev!=$2) {print $0; prev=$2} else {prev=$2}}' -
About the double sort: this should work to a certain extent with a double range declaration. But I'm not sure if the second sort really does a dictionary sort or converts the chars to an ascii value and sorts them numerically.
E.g.:
cat your_file | sort -t, -n +1 -2 -d +4 -5
Regards,
Vincent
let's give this a try:
cat your_file | sort -t, -n +1 -2 | awk -F, 'BEGIN {prev="" } {if (prev!=$2) {print $0; prev=$2} else {prev=$2}}' -
About the double sort: this should work to a certain extent with a double range declaration. But I'm not sure if the second sort really does a dictionary sort or converts the chars to an ascii value and sorts them numerically.
E.g.:
cat your_file | sort -t, -n +1 -2 -d +4 -5
Regards,
Vincent
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Events and news
Customer resources
© Copyright 2025 Hewlett Packard Enterprise Development LP