Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to sort a file using sort command..

I would like to sort the file based on 2nd field which is seperated by "," as a delimiter

Ex: filename1, 465, output_file1,

filename2, 300, output_file2,

Expected output :

filename1, 300, output_file,

filename1, 465, output_file,

I tried this command

sort -t, +1 -2 <file.txt>

But this didn't help. Please let me know if I am missing something

thanks Kiran

share|improve this question

migrated from stackoverflow.com Feb 23 '10 at 13:22

This question came from our site for professional and enthusiast programmers.

GNU sort:

sort -t, -k2 -n file.txt
share|improve this answer
    
Thanks, It worked. – Kiran Feb 23 '10 at 9:09
    
@Kiran : please mark the answer as accepted, then :) – Valentin Rocher Feb 23 '10 at 9:21

You should use sort -t, -nk 2 -s file.txt

-n specifies it as numeric sort and -k specifies key to be used for sorting The +1 etc., options are not supported now.

share|improve this answer

Adding -k 2 to specify field 2 should do it:

sort -t , -k 2 $file

Not sure what +1 -2 comes from. Also, this should probably be on http://superuser.com/.

share|improve this answer

The command below will do it for you, nice and simple :-)

sort -t, -k 2n,2 t.txt

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .