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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
05-25-2012, 09:20 PM
|
#1
|
LQ Newbie
Registered: May 2012
Posts: 8
Rep: 
|
sorting quandry
I am attempting to understand the use of the command sort followed by the flag/switch/option -k. I understand that it sorts the results based on a particular column e.g. if I run the command ls -l | sort -k 2, it would sort the results by the number of links in ascending order.
What I don't quite get is what it means to use position 1 followed by position 2 e.g. ls -l | sort -k 2,2.
The other thing I also noticed is if I run the command ls -l | sort -k 1,5 it sorts the results in descending order whereby the command ls -l | sort -k 5 sorts by ascending order.
|
|
|
Click here to see the post LQ members have rated as the most helpful post in this thread.
|
05-26-2012, 03:38 AM
|
#2
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
The -k 2 option tells sort to start sorting at column 2 and sort the rest of the line and -k 2,2 sorts column 2 only. You won't see much of a difference when using just one -k option. You will see the difference when sorting multiple columns.
Have a look at this:
Code:
$ cat infile
a Z 2
a Z 1
a Y 2
a Y 1
b X 2
b X 1
b W 2
b W 1
$ sort -k 2 infile
b W 1
b W 2
b X 1
b X 2
a Y 1
a Y 2
a Z 1
a Z 2
$ sort -k 2,2 infile
b W 1
b W 2
b X 1
b X 2
a Y 1
a Y 2
a Z 1
a Z 2
$ sort -k 2,2 -k3,3r infile
b W 2
b W 1
b X 2
b X 1
a Y 2
a Y 1
a Z 2
a Z 1
The first 2 sort commands have the same output, but the third first sorts on column 2 and then sorts column 3 (reverse).
Do have a look at info sort, it holds multiple examples and explanations (also a few "complicated" sort examples).
|
|
2 members found this post helpful.
|
05-28-2012, 05:37 PM
|
#3
|
LQ Newbie
Registered: May 2012
Posts: 8
Original Poster
Rep: 
|
@druuna - Thanks. When you say that that it only sorts by column 2 if I use the flag/switch/option -k 2,2 what do you mean exactly? Does this mean if I have 3 columns and use the command sort -k 2,2 only column 2 is sorted however none of the other columns are sorted? From the example you gave that doesn't appear to be the case. Why would I use sort -k 2 over sort -k 2,2 if they produce the same results?
|
|
|
05-28-2012, 06:36 PM
|
#4
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,425
|
As druuna said, the best thing to do is have a read of eg http://linux.die.net/man/1/sort and play with it. There's a million qns you could ask; its going to be a heck of a lot quicker if you just experiment.
If you then still have qns, feel free to come back and ask them.
This is how we've all learnt.

|
|
|
05-29-2012, 03:39 AM
|
#5
|
LQ Newbie
Registered: May 2012
Posts: 8
Original Poster
Rep: 
|
Quote:
Originally Posted by chrism01
As druuna said, the best thing to do is have a read of eg http://linux.die.net/man/1/sort and play with it. There's a million qns you could ask; its going to be a heck of a lot quicker if you just experiment.
If you then still have qns, feel free to come back and ask them.
This is how we've all learnt.

|
Thanks chrism01. I did experiment before posting my question. What I don't quite get is why would I use one over the other e.g. sort -k 2 over sort -k 2,2 or vice versa?
|
|
|
05-29-2012, 04:26 AM
|
#6
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Quote:
Originally Posted by peanutsmonkey
What I don't quite get is why would I use one over the other e.g. sort -k 2 over sort -k 2,2 or vice versa?
|
You need to use that was is appropriate for your situation.
- If you only need to sort entries starting at a certain point, you use -kX
- If you need to sort on multiple columns you use -kX,X -kY,Y
Have a look at this:
Code:
$ cat infile
ccc UUU 111
ccc UUU 111
ccc VVV 111
ccc VVV 222
bbb WWW 222
bbb WWW 222
bbb XXX 333
bbb XXX 333
aaa YYY 333
aaa YYY 444
aaa ZZZ 444
aaa ZZZ 444
Using sort -k2 -k3r -k1 infile produces this:
Code:
ccc UUU 111
ccc UUU 111
ccc VVV 111
ccc VVV 222
bbb WWW 222
bbb WWW 222
bbb XXX 333
bbb XXX 333
aaa YYY 333
aaa YYY 444
aaa ZZZ 444
aaa ZZZ 444
And using sort -k2,2 -k3,3r -k1,1 infile produces this:
Code:
ccc UUU 111
ccc UUU 111
ccc VVV 222
ccc VVV 111
bbb WWW 222
bbb WWW 222
bbb XXX 333
bbb XXX 333
aaa YYY 444
aaa YYY 333
aaa ZZZ 444
aaa ZZZ 444
The difference is caused by limiting the sort to a specific column (-kX,X) and not the complete line starting from a specific column (-kX).
|
|
|
06-03-2012, 05:16 PM
|
#7
|
LQ Newbie
Registered: May 2012
Posts: 8
Original Poster
Rep: 
|
Quote:
Originally Posted by druuna
You need to use that was is appropriate for your situation.
- If you only need to sort entries starting at a certain point, you use -kX
- If you need to sort on multiple columns you use -kX,X -kY,Y
Have a look at this:
Code:
$ cat infile
ccc UUU 111
ccc UUU 111
ccc VVV 111
ccc VVV 222
bbb WWW 222
bbb WWW 222
bbb XXX 333
bbb XXX 333
aaa YYY 333
aaa YYY 444
aaa ZZZ 444
aaa ZZZ 444
Using sort -k2 -k3r -k1 infile produces this:
Code:
ccc UUU 111
ccc UUU 111
ccc VVV 111
ccc VVV 222
bbb WWW 222
bbb WWW 222
bbb XXX 333
bbb XXX 333
aaa YYY 333
aaa YYY 444
aaa ZZZ 444
aaa ZZZ 444
And using sort -k2,2 -k3,3r -k1,1 infile produces this:
Code:
ccc UUU 111
ccc UUU 111
ccc VVV 222
ccc VVV 111
bbb WWW 222
bbb WWW 222
bbb XXX 333
bbb XXX 333
aaa YYY 444
aaa YYY 333
aaa ZZZ 444
aaa ZZZ 444
The difference is caused by limiting the sort to a specific column (-kX,X) and not the complete line starting from a specific column (-kX).
|
Thanks druuna. Can you give me an example of an appropriate situation? Sorry for being a noob but why would I want to sort each column independently of one another?
|
|
|
All times are GMT -5. The time now is 03:12 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|