Search code examples
bashmacossortingcarriage-returnnumerical

sorting numerically by first row


I have a file with almost 900 lines in excel that I've saved as a tab deliminated .txt file. I'd like to sort the text file by the numbers given in the first column (they range between 0 and 2250). The other columns are both numbers and letters of varying length eg.

myfile.txt:

0251  abcd 1234,24 bcde
2240  efgh 2345,98 ikgpppm
0001  lkjsi 879,09 ikol

I've tried

sort -k1 -n myfile.txt > myfile_num.txt

but I just get an identical file with new name. I'd like to get:

myfile_num.txt

0001  lkjsi 879,09 ikol 
0251  abcd 1234,24 bcde
2240  efgh 2345,98 ikgpppm

What am I doing wrong? I'm guessing that it's quite simple, but I'd appreciate any help I can get! I only know a little bash scripting, so it'd be nice if the script is a very simple one-liner that I can understand :)

Thanks :)


Solution

  • Use this to convert old Mac OS carriage return to newline:

    tr '\r' '\n' < myfile.txt | sort