我正在尝试在第二列中使用浮点数(全部介于0和1之间)对制表符分隔的文件进行排序。不知道为什么数字排序在这里不能正常工作:
!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
但是当我第一次使用剪切时它正在工作
!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
答案 0 :(得分:2)
使用cut
,tab是默认分隔符。 sort
不是这样,所以你必须指定它。此外,您需要提供文字制表符,而不仅仅是其他工具接受的双字符字符串\t
。
假设你的shell是bash
sort -t $'\t' -k2,2nr
$'\t'
是bash' ANSI-C quoting