Unix Sort command

Started by
8 comments, last by Instigator 17 years, 5 months ago
I've been learning how to script in unix today. I came across one problem. Let's say you have a file called 'Canada.ca' and it contains the following:
Quote: Edmonton:3183312:661848:September 1, 1905:9 Charlottetown:137900:5660:July 1, 1873:2
I would sort it alphabetically like this: 'sort Canada.ca'. (it would take Edmonton and compare with Charlottetown). Now my question is how do I sort things using the 'deliminer (the ':'). So lets say I want to sort the second field and not the first field. Any ideas on how I would do this?
Advertisement
If you want to learn Unix scripting, let me introduce to Unix' most powerful command : man. In particular, check man sort for -k and -t
LOL! Total Unix dork answer. Good job! "I'm going to pretend I know the answer and tell you to use MAN to look it up."

Unix geeks are all the same. No brains, no girlfriend, no life.

For what it's worth, 'awk' will do what you want. Use it to get the 2nd field from each line, then use your sort on that.

Quote:Original post by Anonymous Poster
Unix geeks are all the same. No brains, no girlfriend, no life.


Actually, I'm a Windows geek, I learned about man from unix geeks around me. But you're right about the rest [grin]

haha.. there's something that guide doesn't have. Sure it tells me how to sort things with a 'deliminer' but it doesn't say how to sort things on the second deliminer.

for example: In order to sort after the first deliminer I would do:
'>sort -t: file.ca'
Note: the ':' on the line above is the deliminer. "States what part of the line to sort.

How do you state. "only sort after second ':'"?

Quote:
Edmonton:3183312:661848:September 1, 1905:9
Charlottetown:137900:5660:July 1, 1873:2
-k.

I appreciate the link you gave me. Obviously I'm too stupid to figure out how to use the manual page. I have no idea why the '-k' command requires

Quote:
-k POS1[,POS2]


I tried getting to the 4th deliminer using:

sort -t: -k4 file.ca

and it doesn't give me any errors. But when I see the output of the sort it's not sorting by using the fourth deliminer. Any ideas?

Sorry for being so stupid.
I suspect that it is sorting the value, just not as you expect it to. It's not going to sort dates correctly (it'll sort months if you ask nicely though). By default, it's alphabetic - so July 1, 2005 is before September 9, 1904.

Sorting dates is quite tricky - ideally, have them all in a consistent format, like, days since a particular point, and then sort them. The Squid Webcache stores it's times as milliseconds since January 1, 1970.

-- Jonathan
EDIT:

You need the '-n' if you're sorting numbers.

Problem solved.
Quote:Original post by Anonymous Poster
LOL! Total Unix dork answer. Good job! "I'm going to pretend I know the answer and tell you to use MAN to look it up."

Unix geeks are all the same. No brains, no girlfriend, no life.

For what it's worth, 'awk' will do what you want. Use it to get the 2nd field from each line, then use your sort on that.


I don't think I want to take this unix course anymore. There's like 40 guys in our class and only 4 women.

3 of which have hairy upper-lips. (not very sexy).

This topic is closed to new replies.

Advertisement