LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-02-2010, 10:30 AM   #1
sorvad
Member
 
Registered: Apr 2004
Location: Cumbria, England
Distribution: openSuSE, Red Hat, HP-UX
Posts: 70

Rep: Reputation: 15
Question Using sort on multiple fields


I am using egrep to list a bunch of lines of code from various cpp and h files.
I want my list to be primarily alphabetically sorted on the name of the file (so the .h file follws its .cpp file) and then numerically by line number.
I am using:
egrep -i -n -T '<str1>|<str2>' *.cpp *.h | sort -t: -k 1 -k 2.1,2.3n
I have tried all sorts of "k" combinations but I always get the files sorted as I want, but the line numbers are alphabetc ie something like 1 10 13 18 2 21 3 etc.
If I put the line numbers as the first key field they come out in the correct order.
I tried it by using cat on the password file and passing it to sort and got similar problems with second field.
I checked various sources and I think it should work.
Any ideas?
 
Old 09-02-2010, 01:01 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Try using sort's -g, -n, or -h options.

Last edited by David the H.; 09-02-2010 at 01:06 PM. Reason: minor update
 
Old 09-03-2010, 05:38 AM   #3
sorvad
Member
 
Registered: Apr 2004
Location: Cumbria, England
Distribution: openSuSE, Red Hat, HP-UX
Posts: 70

Original Poster
Rep: Reputation: 15
Unhappy Sort does not work!

Have tried many variations to -k option. The examples imply that you can sort on more than one key, but I am not convinced!
Here is a little experiment:
List of test file:
$> cat test.dat
aaaa 2
aaaa 4
aaaa 21
aaaa 12
aaaa 1002
bbbb 30
bbbb 3
bbbb 20
Sort by second numeric field as primary - works OK
$> cat test.dat | sort -k 2n
aaaa 2
bbbb 3
aaaa 4
aaaa 12
bbbb 20
aaaa 21
bbbb 30
aaaa 1002
Sort by first alpha field as primary then by numerical field - fails
$> cat test.dat | sort -k 1 -k 2n
aaaa 1002
aaaa 12
aaaa 2
aaaa 21
aaaa 4
bbbb 20
bbbb 3
bbbb 30

Last edited by sorvad; 09-03-2010 at 05:39 AM.
 
Old 09-03-2010, 09:25 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Ah, ok. I see what you mean. That was a tricky one. I had to go to the sort info page to figure it out.
Code:
$ cat test.dat | sort -k1,1 -k2,2n
aaaa 2
aaaa 4
aaaa 12
aaaa 21
aaaa 1002
bbbb 3
bbbb 20
bbbb 30
When sorting by multiple columns, using only -k1 messes it up because that sorts the value from the beginning of the field to the end of the line. You have to tell it to focus only on the current field.

Note that in this example the full -k2,2 is not really necessary, since there are no following fields after it that need to be ignored. Only the first field needs the full specification. But it would be needed if you wanted to further sort the data by a third field.
 
1 members found this post helpful.
Old 09-06-2010, 01:59 AM   #5
sorvad
Member
 
Registered: Apr 2004
Location: Cumbria, England
Distribution: openSuSE, Red Hat, HP-UX
Posts: 70

Original Poster
Rep: Reputation: 15
Many thanks David the H. That works exactly how I wanted it to.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sort by multiple columns wakatana Linux - Newbie 5 10-18-2009 03:35 PM
How to sort a csv on three fields ? (cygwin user) turquoise_man Linux - Newbie 2 08-24-2009 09:12 AM
Unix sort on multiple fields belorion Programming 8 11-12-2007 01:15 PM
Joining multiple lines and summing fields elconde Programming 1 02-13-2004 10:42 PM
Handling multiple forms with same name hidden fields coolman0stress Programming 5 09-04-2003 01:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:14 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
Open Source Consulting | Domain Registration