Operating System - Linux
1753913 Members
8682 Online
108810 Solutions
New Discussion юеВ

Re: Sorting a list by a field

 
SOLVED
Go to solution
Dennis Handly
Acclaimed Contributor

Re: Sorting a list by a field

>James: I think my original post is indeed correct. You can test with additional data as you please.

There is a difference between works and correct.

You say the field ends in field 7, position 10. But there is only one field. There are no other fields that could be added that would cause the sort to mysteriously fail, so it works. (Unless you remove the "n".)

I also don't see that "n" is allowed before the position, it should be at the end:
-k1.7n,1.10n

James R. Ferguson
Acclaimed Contributor

Re: Sorting a list by a field

Hi Dennis:

Your points are well-taken and I wasn't trying to be glib.

> You say the field ends in field 7, position 10. But there is only one field. There is no other fields that could be added that would cause the sort to mysteriously fail, so it works.

I agree. If you had data like this (file):

03/03/2006,1
01/03/2007,2
11/11/2006,3
10/10/2005,4
04/02/2006,5
02/04/2006,6
4/ 4/2004,7
5/5/2005 ,8
05/06/2005,9

Hence, with the default delimiter in force (a blank) I want to limit the key as I originally wrote:

# sort -kn1.7,1.10 -kn1.1,1.2 -kn1.3,1.4 file

(not)

# sort -kn1.7 -kn1.1,1.2 -kn1.3,1.4 file

> ...also [I] don't see that "n" is allowed before the position, it should be at the end:
-k1.7n,1.10n

Yes, I guess that does seem more reasonable. The 'sort' manpages note:

/*begin_quote*/

The arguments field_start and field_end each have the form m.n which are optionally followed by one or more of the type options b, d, f, i, n, r, or M. These modifiers have the functionality for this key only, that their command-line counterparts have for the entire record.

/*end_quote*/

So, are we closer in agreement?

Regards!

...JRF...

Dennis Handly
Acclaimed Contributor

Re: Sorting a list by a field

>James: I want to limit the key as I originally wrote:
# sort -kn1.7,1.10 -kn1.1,1.2 -kn1.3,1.4 file

Both Ralph and I agree with that. Except you originally wrote:
# sort -kn1.7,7.10 ...

With 7.10 instead of 1.10.
James R. Ferguson
Acclaimed Contributor

Re: Sorting a list by a field

Hi Dennis & Ralph:

Holy smoke...I had to look at my first post *twice* to see my error. You and Ralph have sharp eyes!

*Now* I see what you did. Spock's mind-meld would be easier :-))

My thanks, and my apologies to both you & Ralph!

Regards!

...JRF...
Arturo Galbiati
Esteemed Contributor

Re: Sorting a list by a field

Hi,
to have a list of dates (last field of the record) sorted you can use:

awk '{print $NF}' file |sort -t "/" -kn3 -kn1 -kn2

HTH,
Art
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