LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-06-2007, 10:22 AM   #1
iggi
LQ Newbie
 
Registered: Nov 2007
Posts: 29

Rep: Reputation: 16
Sort using keys


Hello,

I've read about 'sort' and am unable to figure out the difference between eg.

"sort -t: -k 2,3" and "sort -t: -k 2"

say <file> contains:

Code:
[root@localhost ~]# cat file
x:1:1:operator:/root:/sbin/nologin
x:1:1:oink:/bin:/sbin/nologin
sort -t: -k 2,3 gives
Code:
x:1:1:oink:/bin:/sbin/nologin
x:1:1:operator:/root:/sbin/nologin
and

sort -t: -k 2

does the same job. I would expect "-k 2,3" NOT to look at the following characters but it still does (oink is put before operator). I read -k 2 processes from field 2 till the end so that result is as expected.

Any idea?

thanks, Dirk
 
Old 11-06-2007, 12:18 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
-k 2,3 will ONLY sort by 2 and 3
-k 2 will sort by everything from the 2nd field onward

If you want to sort by just the 2nd field it's
-k 2,2

The whole line is a "bonus", I guess, because there's
no sort-order defined past your description.

Cheers,
Tink

Last edited by Tinkster; 11-06-2007 at 12:20 PM.
 
Old 11-07-2007, 02:12 AM   #3
iggi
LQ Newbie
 
Registered: Nov 2007
Posts: 29

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by Tinkster View Post
-k 2,3 will ONLY sort by 2 and 3
-k 2 will sort by everything from the 2nd field onward

If you want to sort by just the 2nd field it's
-k 2,2

The whole line is a "bonus", I guess, because there's
no sort-order defined past your description.

Cheers,
Tink
Thanks. Although it's not really an issue I'm trying to understand the difference between "-k x" , "-k x,x" and "-k x,y".

From what I see there's no difference at all and suprisingly it doesn't seem usefull (except eg. "-k x -k y" when column x and y aren't adjacent)
Code:
[root@localhost ~]# cat file
vb test cfg
vb test abc
ab cd   ef
zz zzz zz
vb test bdd
kk zzz zz
[root@localhost ~]# sort -k 1 file
ab cd   ef
kk zzz zz
vb test abc
vb test bdd
vb test cfg
zz zzz zz
[root@localhost ~]# sort -k 1,1 file
ab cd   ef
kk zzz zz
vb test abc
vb test bdd
vb test cfg
zz zzz zz
[root@localhost ~]# sort -k 2 file
ab cd   ef
vb test abc
vb test bdd
vb test cfg
kk zzz zz
zz zzz zz
[root@localhost ~]# sort -k 2,2 file
ab cd   ef
vb test abc
vb test bdd
vb test cfg
kk zzz zz
zz zzz zz
It's all the same and even for "-k 2,2" the previous columns get sorted (for same values of key 2). Even "-k 2,3" produces the same output.

Dirk
 
Old 11-07-2007, 02:18 AM   #4
iggi
LQ Newbie
 
Registered: Nov 2007
Posts: 29

Original Poster
Rep: Reputation: 16
I guess the only reason would be when appending options like 'g' or 'n' to specify the type of sorting. Then you could say eg. column 2 and 3 contain numbers but the rest is just text:

-k 2,3n
 
Old 11-07-2007, 02:31 AM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Those are fair enough comments. But just go the other way around;
what behaviour would you expect for the other cases? There's no
real way (or need) to "unsort" things if there's no second condition
that is indeed of a different type (or direction) or in non-adjacent
columns ... of course you can also go like

sort -k3,3g -k1,2r -k4

and the likes, which should be bear quite different results.



Cheers,
Tink
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a line limit with the sort utility? Trying to sort 130 million lines of text gruffy Linux - General 4 08-10-2006 09:40 PM
Keys in bind -p or inputrc, what do these keys translate to? muha Linux - Newbie 0 03-06-2006 07:52 AM
YUM requires public keys, how do I get public keys? GNUROCKS Linux - Newbie 3 05-29-2005 09:50 AM
with keys is the keys that change languages? AKAKAK Fedora 2 01-25-2005 11:11 AM
Configuring SSH to accept only keys (already have keys) fr0st Linux - Security 3 11-04-2003 04:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration