0

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

4 Answers 4

1

GNU sort:

sort -t, -k2 -n file.txt
0
1

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.

0

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 https://superuser.com/.

0

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

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

You must log in to answer this question.

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