Operating System - Linux
1827849 Members
1949 Online
109969 Solutions
New Discussion

Re: Sorting a list by a field

 
SOLVED
Go to solution
Sandman!
Honored Contributor

Re: Sorting a list by a field

>xxx xxx xxx xxx xxx xxx xxxx xxxx 02/11/07
>xxx xxx xxx xxx xxx xxx xxxx 02/02/07

>So they don't sort right.
>The only thing that I do know is that it's always the last field. I cannot think how >to make awk or sort work on this.

Try the shell script below if your issue has not been resolved yet. The script below takes the input file ($1) as its only argument and ends up modifying it with the lines sorted chronologically. Invoke as follows:

# ./sortdate.sh inputfile

=======================sortdate.sh=======================

#!/usr/bin/sh

awk '{split($NF,z,"/");l=z[3]""z[2]""z[1];print l,$0}' $1 | sort -nk1,1 |
awk '{for(i=2;i<=NF;++i) printf(i $1
Sandman!
Honored Contributor

Re: Sorting a list by a field

Minor correction to my last post; use the script below instead.

=======================sortdate.sh=======================

#!/usr/bin/sh

awk '{split($NF,z,"/");l=z[3]""z[1]""z[2];print l,$0}' $1 | sort -nk1,1 |
awk '{for(i=2;i<=NF;++i) printf(i $1
Marlou Everson
Trusted Contributor

Re: Sorting a list by a field

If the field is mm/dd/yy, then the sort key for the dd part should be 1.4,1.5 not 1.3,1.4.

Marlou
dictum9
Super Advisor

Re: Sorting a list by a field



On a different note, why doesn't the following sort the file by the last 2 numeric characters?

# cat file
bd0hbsv1 CNAME expconserv2-port20
bd0hbsv2 CNAME expconserv2-port09
bd0hbsv3 CNAME expconserv2-port11
bd0ints2 CNAME expconserv2-port38

# cat file | sort -n -k 3.17,3.18
bd0hbsv1 CNAME expconserv2-port20
bd0hbsv2 CNAME expconserv2-port09
bd0hbsv3 CNAME expconserv2-port11
bd0ints2 CNAME expconserv2-port38
Hein van den Heuvel
Honored Contributor

Re: Sorting a list by a field

It doesn't? It seems to sort fine for me.
There must be a non-printable in the stream.
Or maybe a ^M stripped of in the forum paste.

Check out the file with #xd -t c file

btw... why use 'cat + pipe'?
sort is perfectly capable of reading a file directly!

If you still have trouble, be sure to attache the actual file as a .TXT to a next reply.

Regards,
Hein.

$ sort -n -k 3.17,3.18 file
01234567890123456789012345678901234567890123456789
bd0hbsv2 CNAME expconserv2-port09
bd0hbsv3 CNAME expconserv2-port11
bd0hbsv1 CNAME expconserv2-port20
bd0ints2 CNAME expconserv2-port38
James R. Ferguson
Acclaimed Contributor

Re: Sorting a list by a field

Hi:

Regarding your last question, it works for me. By the way, you don't need the extra 'cat' process! Simply:

# sort -n -k 3.17,3.18 file > file.out

Regards!

...JRF...
Peter Nikitka
Honored Contributor

Re: Sorting a list by a field

Hi,

did you try to include the 'blank field skipping option' for your latest try?

sort -nb -k 3.17,3.18 file

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"