LinuxQuestions.org
Help answer threads with 0 replies.
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 05-25-2012, 09:20 PM   #1
peanutsmonkey
LQ Newbie
 
Registered: May 2012
Posts: 8

Rep: Reputation: Disabled
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.
Old 05-26-2012, 03:38 AM   #2
anon237
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
Old 05-28-2012, 05:37 PM   #3
peanutsmonkey
LQ Newbie
 
Registered: May 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
@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?
 
Old 05-28-2012, 06:36 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,425

Rep: Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786Reputation: 2786
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.
 
Old 05-29-2012, 03:39 AM   #5
peanutsmonkey
LQ Newbie
 
Registered: May 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by chrism01 View Post
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?
 
Old 05-29-2012, 04:26 AM   #6
anon237
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by peanutsmonkey View Post
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).
 
Old 06-03-2012, 05:16 PM   #7
peanutsmonkey
LQ Newbie
 
Registered: May 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
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?
 
  


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
[SOLVED] autoFS / NFS Quandry merana Solaris / OpenSolaris 2 11-22-2010 07:27 AM
GNU Cash Quandry Slokunshialgo Linux - Software 2 01-24-2008 10:09 PM
awk quandry methodtwo Programming 4 07-05-2007 06:02 PM
.htaccess quandry slybob Linux - Server 6 02-05-2007 10:26 AM
IPtables Quandry caps_phisto Linux - Security 1 04-18-2005 02:19 AM

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

All times are GMT -5. The time now is 03:12 AM.

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
Open Source Consulting | Domain Registration