LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 07-03-2008, 01:31 AM   #1
djfog
Member
 
Registered: Jun 2008
Distribution: Linux Mint 4.0(Daryna)
Posts: 31

Rep: Reputation: 15
Sort command


Hi to all.My question is how I can use sort with two references.Example I want to sort a file named Mail.txt by surname and name,if I have more than a time the same surname.Thanks in advance..
 
Old 07-03-2008, 01:36 AM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Show examples of what the text input looks like. Sounds like you want two sort keys, a primary based on surname, and secondary based on first name.
 
Old 07-03-2008, 01:39 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
The "info sort" manual has examples using two or more keys.
 
Old 07-03-2008, 01:51 AM   #4
djfog
Member
 
Registered: Jun 2008
Distribution: Linux Mint 4.0(Daryna)
Posts: 31

Original Poster
Rep: Reputation: 15
This is the text:
Code:
George Pappas 12136 Peristeri 5757675
Nick Nikolaoy 12232 Aigaleo 5314555
John Ioannoy 13222 Athens 3245890
Nick Pappas 11223 Aigaleo 5324123
George Georgioy 11132 Athens 3245678
Helen Georgioy 12136 Peristeri 5748456
Nick Pappas 11223 Aigaleo 5324123
John Ioannoy 13222 Athens 3245890
Helen Thanoy 11132 N.Smyrni 9718345
where the 2nd column is the surname and the first the name.I'm from Greece as you see.
 
Old 07-03-2008, 01:54 AM   #5
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Use the -k option to indicate which field contains the key. Since your data is space-separated, which is the default field for sort, the sort is trivial:

sort -k2 -k1


Χαίρετε

Last edited by Mr. C.; 07-03-2008 at 02:16 AM.
 
Old 07-03-2008, 02:05 AM   #6
djfog
Member
 
Registered: Jun 2008
Distribution: Linux Mint 4.0(Daryna)
Posts: 31

Original Poster
Rep: Reputation: 15
I did not quite understand.I used sort -2 -1 Mail.txt but I get an error message saying invalid option --2.Could you be more specific..?


Έλληνας..?
 
Old 07-03-2008, 02:14 AM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Notice the letter "k" in the example.
 
Old 07-03-2008, 02:14 AM   #8
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
You need the -k to preceed the key number:

Code:
$ cat in3
George Pappas 12136 Peristeri 5757675
Nick Nikolaoy 12232 Aigaleo 5314555
John Ioannoy 13222 Athens 3245890
Nick Pappas 11223 Aigaleo 5324123
George Georgioy 11132 Athens 3245678
Helen Georgioy 12136 Peristeri 5748456
Nick Pappas 11223 Aigaleo 5324123
John Ioannoy 13222 Athens 3245890
Helen Thanoy 11132 N.Smyrni 9718345

$ sort -k2 -k1 in3
George Georgioy 11132 Athens 3245678
Helen Georgioy 12136 Peristeri 5748456
John Ioannoy 13222 Athens 3245890
John Ioannoy 13222 Athens 3245890
Nick Nikolaoy 12232 Aigaleo 5314555
Nick Pappas 11223 Aigaleo 5324123
Nick Pappas 11223 Aigaleo 5324123
George Pappas 12136 Peristeri 5757675
Helen Thanoy 11132 N.Smyrni 9718345
Αμερικάνος, ανόητος.
 
Old 07-03-2008, 02:16 AM   #9
djfog
Member
 
Registered: Jun 2008
Distribution: Linux Mint 4.0(Daryna)
Posts: 31

Original Poster
Rep: Reputation: 15
Thanks.It really works...
 
Old 07-03-2008, 02:17 AM   #10
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
By the way, that was me calling myself a silly american!
 
Old 07-03-2008, 02:22 AM   #11
djfog
Member
 
Registered: Jun 2008
Distribution: Linux Mint 4.0(Daryna)
Posts: 31

Original Poster
Rep: Reputation: 15
And how can I erase the double names..??
 
Old 07-03-2008, 02:28 AM   #12
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
pipe everything to uniq

sort ... | uniq

that's all there is to it.
 
Old 07-03-2008, 02:32 AM   #13
djfog
Member
 
Registered: Jun 2008
Distribution: Linux Mint 4.0(Daryna)
Posts: 31

Original Poster
Rep: Reputation: 15
Thanks for your help.I can now suppose that if I want to sort surname and the 5 digit mail code the sort command would be like this:
sort -k3 -k2 Mail.txt
Am I right..?
Thanks again..?


Γιατί ανόητος Αμερικάνος??
 
Old 07-03-2008, 02:35 AM   #14
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Well, numeric sorts are different than character sorts. You add options to the end of the sort key to change the type of sort. For example, just sorting numerically on the last field:

sort -k5n

Note the addition of "n" to the -k5 key field.

έξοχος ερώτημα !

Last edited by Mr. C.; 07-03-2008 at 02:38 AM.
 
Old 07-03-2008, 02:40 AM   #15
djfog
Member
 
Registered: Jun 2008
Distribution: Linux Mint 4.0(Daryna)
Posts: 31

Original Poster
Rep: Reputation: 15
I just noticed something...the command
Quote:
sort -k2 -k1
does not seem to work properly as George Pappas is under Nick Pappas and I want the opposite.Any ideas..?

Last edited by djfog; 07-03-2008 at 02:45 AM.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Explain sort command viveksnv Programming 3 02-27-2008 09:08 PM
Sort command arya6000 Linux - Newbie 2 11-27-2007 07:50 PM
Sort Command saravanan1979 Programming 1 10-03-2004 11:36 AM
The SORT command Rezon Programming 2 10-30-2003 04:14 PM
Using the Sort command in vi timnphx Programming 2 04-06-2001 11:39 PM

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

All times are GMT -5. The time now is 02:13 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