Jump to content
  • Advertisement
Sign in to follow this  
Instigator

Unix Sort command

This topic is 4406 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
Guest 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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!