darren17
darren17

Reputation: 235

Unix sort not working without using cut

I'm trying to sort a tab-delimited file with floats (all between 0 and 1) in the second column. Not sure why numeric sort isn't working properly here:

!cat test_pairs.txt | sort -k2,2nr

["the", "before"]   0.2362988020937804
["the", "consciousness"]    0.10186856794636664
["the", "depend"]   0.23407990656112676
["the", "grew"] 0.2223885986835286
["the", "judgment"] 0.22695970527952097
["the", "one's"]    0.6370648872479585
["the", "phenomenon"]   0.3220069716292701
["the", "pieces"]   0.5419993110593677
["the", "unto"] 0.14628736468011103
["the", "youth"]    0.0004759166055388365

But it is working when I use cut first

!cat test_pairs.txt | cut -f2 | sort -nr

0.6370648872479585
0.5419993110593677
0.3220069716292701
0.2362988020937804
0.23407990656112676
0.22695970527952097
0.2223885986835286
0.14628736468011103
0.10186856794636664
0.0004759166055388365

Upvotes: 0

Views: 1063

Answers (1)

glenn jackman
glenn jackman

Reputation: 247012

With cut, tab is the default delimiter. Not so with sort, so you have to specify it. Also, you need to provide a literal tab character, not just the two-character string \t that other tools accept.

Assuming your shell is bash

sort -t $'\t' -k2,2nr

$'\t' is bash's ANSI-C quoting

Upvotes: 2

Related Questions