Register
It is currently Sat Nov 01, 2014 2:09 am

Sort question


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Thu Oct 30, 2014 7:57 pm   

Joined: Thu Oct 30, 2014 7:35 pm
Posts: 2
Im taking a Linux essentials class. In a lab, Im asked to sort by the last three digits of the zip code.

Example data
Quote:
Tom Winstrom 94201
Janet Dempsey 94111
Alice MacLeod 94114
David McKenzie 94114
Toni MacDonald 95020
Jack Cooper 94072
Richard Ziff 95510


I know the form has to be something like this as per the info page.
Code:
  * Sort numerically on the second field and resolve ties by sorting
     alphabetically on the third and fourth characters of field five.
     Use `:' as the field delimiter.

          sort -t : -k 2,2n -k 5.3,5.4


This makes sense
Code:
-Inspiron-6000:~$ cat zips
94201
94111
94114
94114
95020
94072
95510
-Inspiron-6000:~$ sort -k1.3,1.5 zips
95020
94072
94111
94114
94114
94201
95510



i thought the following would do it
Code:

-Inspiron-6000:~$ sort -n -k3,3 -k3.3,3.5 ziplist
Jack Cooper 94072
Janet Dempsey 94111
Alice MacLeod 94114
David McKenzie 94114
Tom Winstrom 94201
Toni MacDonald 95020
Richard Ziff 95510


I cant see how to say move to field 3 and sort by char 3-5. ... driving me nuts !! :-/, I searched the web, hit Linux in a nutshell, reviewed class material. Just cant see it !!

Appreciate any help.

Tks
Pop


Top
 Profile  
 PostPosted: Fri Oct 31, 2014 6:01 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 552
hi,

from man sort:
Quote:
characters in a field are counted from the beginning of the preceding whitespace.
this seems to mean that preceding whitespace is counted as first character.
so:
Code:
sort -k3.4n file
?


Top
 Profile  
 PostPosted: Fri Oct 31, 2014 8:40 am   

Joined: Thu Oct 30, 2014 7:35 pm
Posts: 2
Thanks alot Wat. Your solutions does work. But it does raise another issue.
Playing around a little so that I gain further understanding ... one of those kind of guys wanting the knowledge not the grade .. know ?

Why doesn't this work ... I would expect that the character sort would shift/execute on the second to last digit because the -b says ignore leading white space.

Code:
school]$ sort -b -k3.4n list
Toni MacDonald 95020
Jack Cooper 94072
Janet Dempsey 94111
Alice MacLeod 94114
David McKenzie 94114
Tom Winstrom 94201
Richard Ziff 95510


Top
 Profile  
 PostPosted: Fri Oct 31, 2014 4:08 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 552
add the `--debug' option to see what's happening.
then try
Code:
sort -k3.3bn --debug yourFile
and see the differences.

the `info sort' page has more details about `sort'.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Yahoo [Bot] and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP