Operating System - HP-UX
1753903 Members
9266 Online
108810 Solutions
New Discussion юеВ

sort via multiple keyfields

 
SOLVED
Go to solution
Boyd Kodama
Frequent Advisor

sort via multiple keyfields

Trying to sort the following listing (ls):

file1.10
file1.11
file1.8
file1.9
file2.1
file2.10
file2.2
file2.3
file3.1
file3.10
file3.2

so the result is:

file1.8
file1.9
file1.10
file1.11
file2.1
file2.2
file2.3
file2.10
file3.1
file3.2
file3.10

Already tried `ls | sort -n -t.` which sorts the first
field only and `ls | sort -n -t. +1` which sorts the second field only.

Also tried `ls | sort -n -t. -k 1,2` which sorts the
second field only.

Any ideas?

thanks,

Boy
Without Mondays, there weren't be any Fridays
4 REPLIES 4
John Strang
Regular Advisor

Re: sort via multiple keyfields

Hi Boyd,

Try redirecting the output of the first sort into the second, as follows:

`ls | sort -n -t. | sort -n -t. +1`

This uses both the sorts you have already tried, but combining them together.

I'll try it later today and let you know how it goes....

John
If you never make a mistake you'll never make anything.
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: sort via multiple keyfields

This should do it:

cat myfile | sort -t '.' -k 1,1b -k 2,2n > newfile
If it ain't broke, I can fix that.
Boyd Kodama
Frequent Advisor

Re: sort via multiple keyfields

Thanks Clay. Yours worked.

Sorry John, the second `sort`
re-sorts the whole list according to the second field.

Boyd
Without Mondays, there weren't be any Fridays
John Strang
Regular Advisor

Re: sort via multiple keyfields

Hi Boyd,

Apologies for posting an incorrect solution - I'd not considered the fact that the second sort would re-sort the whole list :-(

John
If you never make a mistake you'll never make anything.