Numeric sort by the column values

numeric datasort

For example I have some test file and want to sort it by the column values. After

awk -F'[:,]' '{print $16}' test.json

I get next output for the column 16:

 123
 457
 68
 11
 939
 11
 345
 9
 199
 13745

Now, when I want to sort it numeric, I use

awk -F'[:,]' '{print $16}' test.json | sort -nk16

but I just get back not nummeric sort…

 11
 11
 123
 13745
 199
 345
 457
 68
 9
 939

What is a reason, I thought -n parameter is enough for the nummeric sort….

Best Answer

The output of awk contains only one column, so no 16th column.

So sort sees all identical empty sort keys and what you observe is the result of the last resort sort (lexical sort on the whole line) which you can disable with the -s option in some implementations.

Here you want:

awk -F'[:,]' '{print $16}' test.json | sort -n

Now, if you want to sort the file on the 16th column, beware sort supports only one character column delimiter, so you'd need to preprocess the input:

sed 's/[:,]/&+/g' test.json | sort -t+ -k16,16n | sed 's/\([:,]\)+/\1/g'

Here appending a + to every : or ,, sorting with + as the column delimiter and removing the + afterwards.

Related Question