I have file1:


File2 I want:


Basically numerically sort of file1.

  • When I run a plain Unix sort on your File1, with no options, I get your File2. What are you doing differently?  What are you leaving out of this question? – Scott Sep 28 '17 at 4:49

I like -V --version-sort: it behaves very well for many situations mixing strings and numbers

sort -V

I use this option very often...

In the some direction, ls -v for version-sort ls.


You need to tell sort -n to sort on the part after the =:

sort -t = -k 2n
  • I recently was looking this ^^. But one more Q. What do -k2n? Is that relate to numeric sort? thank you. – αғsнιη Nov 21 '14 at 10:42
  • 2
    @KasiyA -k defines a sort key. See the man page for details. -k2n defines a sort key starting at the second field and ending at the end of the line and makes it a numeric sort key. – Stéphane Chazelas Nov 21 '14 at 10:45

use sort:

sort -n file1 > file2

-n, --numeric-sort
compare according to string numerical value

 sort -g file1 > file 2

-g, --general-numeric-sort

  • 3
    No, all those lines have the same ranking with sort -n since they don't start with a number. The reason it sorts them is the last-resort full-line sort (lexically, not numerically) done for lines with the same ranking. That would sort "$lvl=17" before "$lvl=2". – Stéphane Chazelas Nov 21 '14 at 10:02
  • @StéphaneChazelas thanks for pointing out. – Hackaholic Nov 21 '14 at 10:12
  • To paraphrase, -n and -g are redundant here as the input is not numerical. So this answer is misleading, hence the downvote (also note that -g and the long options are GNU specific). – Stéphane Chazelas Nov 21 '14 at 10:15
  • but -g is good general-numeric-sort – Hackaholic Nov 21 '14 at 10:16
  • That's still for sorting numerical values, the difference with -n is that it's not limited to decimal integers. That would still sort "$lvl=17" before "$lvl=2" as part of the last-resort sorting. – Stéphane Chazelas Nov 21 '14 at 10:18

I found that, you just run sort -h, it will work. They call it --human-numberic-sort.

sort -h file1 > file2

sort -n is what you want to use. It sorts as we sort numbers. sort -k 2 -n file1

  • 2
    But the OP wants to sort in ascending order, which is the default.  Why are you suggesting reverse order? – G-Man Says 'Reinstate Monica' Sep 13 '17 at 2:21
  • the above is correct when fields are separated with tabs or spaces and will not work in OP's case. – αғsнιη Apr 15 '18 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.