Operating System - Linux
1752795 Members
6249 Online
108789 Solutions
New Discussion юеВ

Re: sort options? easy 10 points!

 
SOLVED
Go to solution
Christian Deutsch_1
Esteemed Contributor

sort options? easy 10 points!

Hi guys and gals,

Here are 10 easy points for you. Can you tell me the sort options I need to use to sort German dates?

Sort these 3 lines:

01.10.2006 x
01.11.2005 y
01.09.2006 z

Result must look like:

01.11.2005 y
01.09.2006 z
01.10.2006 x

Solution must work for (presumably using the sort command with options that work) SuSE Linux Professional 9.3.

The first 3 answers that I can confirm will work for SuSE 9.3 will get 10 points each.

Thanks! Christian
Yeshua loves you!
5 REPLIES 5
Robert Walker_8
Valued Contributor
Solution

Re: sort options? easy 10 points!

Gday,

I Just learnt something. try sort with a few switches:

sort -k1.7,1.11 -k1.4,1.5 -k1.1,1.2 file1 file2

-k sorts on fields
1 is field 1 or the date, (y,z,x) would be field 2.
7 - 11 is YYYY
4 - 5 is MM
1 - 2 is DD

Seems to work including adding exra data to prove the test. Works on RHEL4 and even works on Tru64!

Robert.
Ryan Goh
Frequent Advisor

Re: sort options? easy 10 points!

Steps for the solution:

1. Create a file named file1 contain 3 lines as below.
01.10.2006 x
01.11.2005 y
01.09.2006 z

2. # sort -k 1.7,1.10 file1 (sort by seventh to tenth characters of field one)

3. Result,
01.11.2005 y
01.09.2006 z
01.10.2006 x

regards,
Ryan
Ryan Goh
Frequent Advisor

Re: sort options? easy 10 points!

I would to add something, for sort command -k option, in between -k option and position number you can put a space or no space, it will still work. The solution I give work only based on the content you provide.

I agree with the solution provided by Robert Walker. It sort by 7th to 10th characters of first field, then 4th to 5th characters of first field, follow by 1st to 2nd characters of first field. It will works on any combination of contents.

I already tested on Suse 9.3, it works.

regards,
Ryan
Peter Godron
Honored Contributor

Re: sort options? easy 10 points!

Christian,
if you abored with the straight forward solutions:
tr " " "." < b.lis | sort -t'.' -k3,3 -k2,2 -k1 | sed 's/./ /11'

Using the date separator as a field seperator.
Christian Deutsch_1
Esteemed Contributor

Re: sort options? easy 10 points!

Thanks guys. Now I got it! This is what I used in the end:

sort -n -t'.' -k3 -k2 -k1

I had the additional difficulty of the fields being different lengths e.g. 9 for September and 10 for October so I had to use -n also

Thanks again all of you that was a great help!
Yeshua loves you!