- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Sorting a list by a field
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
Discussions
Discussions
Forums
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
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
тАО02-14-2007 05:38 AM
тАО02-14-2007 05:38 AM
Greetings,
I have a list with one of the field in the mm/dd/yy format and I would like to sort by that field, chronologically. I've read the man page on sort but cannot quite get it to sort correctly. Advice?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-14-2007 05:43 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-14-2007 06:00 AM
тАО02-14-2007 06:00 AM
Re: Sorting a list by a field
OK, given this file:
03/03/2006
01/03/2007
11/11/2006
10/10/2005
04/02/2006
02/04/2006
# sort -kn1.7,7.10 -kn1.1,1.2 -kn1.3,1.4 /tmp/0214b
10/10/2005
02/04/2006
03/03/2006
04/02/2006
11/11/2006
01/03/2007
Regards!
...JRF...
- Tags:
- Sort
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-14-2007 07:01 AM
тАО02-14-2007 07:01 AM
Re: Sorting a list by a field
Thanks.
Actually, in my case, the date is the 8th field on the line, and sometimes it's in the 80th column and sometimes not. So it's not always in the same column.
How do I account for this inconsistency? Can I use awk?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-14-2007 07:14 AM
тАО02-14-2007 07:14 AM
Re: Sorting a list by a field
> Actually, in my case, the date is the 8th field on the line, and sometimes it's in the 80th column and sometimes not. So it's not always in the same column.
How do I account for this inconsistency? Can I use awk?
This doesn't matter. The 'sort' counts its keys from one. Hence '-k1' means the first field and '-k8' would be the eighth. Fields are delimited by whatever you specify in '-t'. Hence '-t' without further qualification means a blank as a field delimiter regarless of what "column" offset.
Have another look at the manpages for 'sort' using the solution I first posted.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-14-2007 08:35 AM
тАО02-14-2007 08:35 AM
Re: Sorting a list by a field
cut -f 1,6 -d: /etc/passwd
-d is the field delimiter. For awk, you specify the delimiter with -F and for sort the field delimiter is set with -t. awk is particularly useful as it predefines variables like NF (number of fields) so regardless of spacing, the last field can be extracted with $NF as in:
awk -F: '{print $1,$NF}' /etc/passwd
which prints the username and the shell.
Now your list may have fields defined with one delimiter such as a comma and within the field, data such as mm/dd/yy that uses / as a separator. That will be a problem because sort cannot understand nested fields. If you can show an example line, perhaps wew can suggest a way to sort it.
Bill Hassell, sysadmin
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-14-2007 07:57 PM
тАО02-14-2007 07:57 PM
Re: Sorting a list by a field
because I am more accustomed to Perl's sort and seldom use Unix sort.
Isn't there a typo in James' solution in the end field specifier of the 1st sort criteria,
and shouldn't it rather be something like:
$ sort -n -k 1.7,1.10 -k 1.1,1.2 -k 1.3,1.4 < /file/to/sort.ascii
I'm not asking to appear a smart-ass
but rather to understand Unix sort better.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-14-2007 08:18 PM
тАО02-14-2007 08:18 PM
Re: Sorting a list by a field
It looks like it. Though I don't think you can have a space after -k and you don't need "<" for file input.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-14-2007 08:29 PM
тАО02-14-2007 08:29 PM
Re: Sorting a list by a field
as it is field 8 and the format is DD/MM/YY should it not be:
sort -n -k 8.7,8.9 -k 8.1,8.2 -k 8.4,8.5
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-14-2007 08:30 PM
тАО02-14-2007 08:30 PM
Re: Sorting a list by a field
as it is field 8 and the format is MM/DD/YY should it not be:
sort -n -k 8.7,8.9 -k 8.1,8.2 -k 8.4,8.5