JESUS_M
JESUS_M

Reputation: 45

Sorting using -k

I tried this solution to my list and I can't get what I want after sorting.

I got list:

m_2_mdot_3_a_1.dat    ro=  303112.12
m_1_mdot_2_a_0.dat    ro=  300.10
m_2_mdot_1_a_3.dat    ro=  221.33
m_3_mdot_1_a_1.dat    ro=  22021.87

I used sort -k 2 -n >name.txt I would like to get list from the lowest ro to the highest ro. What I did wrong? I got a sorting but by the names of 1 column or by last value but like: 1000, 100001, 1000.2 ... It sorted like by only 4 meaning numbers or something.

Upvotes: 0

Views: 45

Answers (2)

ZNZNZ
ZNZNZ

Reputation: 376

cat test.txt | tr . , | sort -k3 -g | tr , .

The following link gave a good answer Sort scientific and float

In brief,

  1. you need -g option to sort on decimal numbers;
  2. the -k option start from 1 not 0;

  3. and by default locale, sort use , as seperator for decimal instead of .

However, be careful if your name.txt contains , characters

Upvotes: 1

pgngp
pgngp

Reputation: 1562

Since there's a space or a tab between ro= and the numeric value, you need to sort on the 3rd column instead of the 2nd. So your command will become:

cat input.txt | sort -k 3 -n

Upvotes: 0

Related Questions