Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
-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.
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:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.