Question

I am trying to sort a table based on the first value from smallest to largest with the gnu-coreutils sort command.

My table looks something like this:

file.txt

100,0.8,0.323, ... some more data
2,0.323,0,323, ...
4, ...
53, ...
.
.
121, ...

I have tried doing the following:

sort -n -k 1 file.txt

but I get things like...

10,0,10,10
100,9,1,10
101,9,2,11
102,9,3,12
103,9,4,13
104,9,5,14
105,9,6,15
106,9,7,16
107,9,8,17
108,9,9,18
21,1,10,11
32,2,10,12
43,3,10,13
54,4,10,14

I want the logical numerical order.

Ted.

Was it helpful?

Solution

The correct invocation of sort might look like

$ sort -n -t , -k 1,1 file.txt

Explanation:

  • -n sort numerically
  • -t , comma is field separator
  • -k 1,1 sort on the first field
  • file.txt input file

Invoking the above given program gives

10,0,10,10
21,1,10,11
32,2,10,12
43,3,10,13
54,4,10,14
100,9,1,10
101,9,2,11
102,9,3,12
103,9,4,13
104,9,5,14
105,9,6,15
106,9,7,16
107,9,8,17
108,9,9,18

When sorting by fields it is very handy to use --debug option, just to make sure that sort works as expected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top