Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting 1 before 10 in bash

Tags:

bash

sorting

I'm sorting numerically on a specific column of a ; separated file with:

sort -k3n file

But numbers 10 and 11 are sorted before 1. How do I fix this?


2 Answers

If your fields are separated with something else than a blank, you can use the --field-separator or -t option, like so:

sort -k3n -t\; file
like image 152
Michał Trybus Avatar answered Oct 22 '25 04:10

Michał Trybus


The -n option should do the trick. Can you check that this doesn't output the order you want?

echo -e "1\n10\n11\n2"|sort -n
like image 40
Miquel Avatar answered Oct 22 '25 06:10

Miquel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!