Operating System - Linux
1751969 Members
4691 Online
108783 Solutions
New Discussion юеВ

Sorting a list by a field

 
SOLVED
Go to solution
dictum9
Super Advisor

Sorting a list by a field


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?
31 REPLIES 31
Pete Randall
Outstanding Contributor
Solution

Re: Sorting a list by a field

Did you try "sort -n"?


Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: Sorting a list by a field

Hi:

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...
dictum9
Super Advisor

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?

James R. Ferguson
Acclaimed Contributor

Re: Sorting a list by a field

Hi (again):

> 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...
Bill Hassell
Honored Contributor

Re: Sorting a list by a field

The definition of a field is that it is an unambiguous element so the concept of columns is unimportant. As long as the definition of each field is unique (ie, each field is separated by a : or , or / etc) then parsing the elements is trivial with many commands like sort, awk, cut, etc. To see this at work, run the cut command to find the user and the user's HOME directory (fields 1 and 6):

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
Ralph Grothe
Honored Contributor

Re: Sorting a list by a field

Just out of curiosity,
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.
Madness, thy name is system administration
Dennis Handly
Acclaimed Contributor

Re: Sorting a list by a field

>Ralph: Isn't there a typo in James' solution

It looks like it. Though I don't think you can have a space after -k and you don't need "<" for file input.
Peter Godron
Honored Contributor

Re: Sorting a list by a field

Hi,
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
Peter Godron
Honored Contributor

Re: Sorting a list by a field

Hi, [ CORRECTION to stated date format ]
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