- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Sorting a list by a field
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-15-2007 04:55 AM - edited тАО10-08-2011 09:07 PM
тАО02-15-2007 04:55 AM - edited тАО10-08-2011 09:07 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-15-2007 05:23 AM
тАО02-15-2007 05:23 AM
Re: Sorting a list by a field
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-15-2007 06:08 AM
тАО02-15-2007 06:08 AM
Re: Sorting a list by a field
# 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-15-2007 06:28 AM
тАО02-15-2007 06:28 AM
Re: Sorting a list by a field
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-15-2007 08:01 PM
тАО02-15-2007 08:01 PM
Re: Sorting a list by a field
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-16-2007 05:06 AM
тАО02-16-2007 05:06 AM
Re: Sorting a list by a field
>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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-16-2007 06:35 AM
тАО02-16-2007 06:35 AM
Re: Sorting a list by a field
=======================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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-21-2007 11:27 AM
тАО02-21-2007 11:27 AM
Re: Sorting a list by a field
Marlou
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2007 09:02 AM
тАО03-29-2007 09:02 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-29-2007 09:13 AM
тАО03-29-2007 09:13 AM
Re: Sorting a list by a field
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
- Tags:
- evil cat