排序命令无法正常工作:
档案:
cat /tmp/2.csv
66.199.199.221,115645,0
207.233.77.147,120167,0
204.38.48.1,125767,0
83.144.97.50,127944,3
12.174.177.15,134080,0
195.76.177.90,138124,0
50.202.17.163,162618,0
66.64.209.30,163729,0
40.76.63.140,181976,0
207.241.237.163,2226,1854
观察行207.241.237.163,2226,1854
:
排序:
sort -t, -nk 2 /tmp/2.csv
66.199.199.221,115645,0
207.233.77.147,120167,0
204.38.48.1,125767,0
83.144.97.50,127944,3
12.174.177.15,134080,0
195.76.177.90,138124,0
50.202.17.163,162618,0
66.64.209.30,163729,0
40.76.63.140,181976,0
207.241.237.163,2226,1854
但使用TSV时效果很好:
sort -nk 2 /tmp/3.tsv
207.241.237.163 2226 1854
66.199.199.221 115645 0
207.233.77.147 120167 0
204.38.48.1 125767 0
83.144.97.50 127944 3
12.174.177.15 134080 0
195.76.177.90 138124 0
50.202.17.163 162618 0
66.64.209.30 163729 0
40.76.63.140 181976 0
使用不同版本的sort进行测试:
sort --version
sort (GNU coreutils) 8.21
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Mike Haertel and Paul Eggert.
并且:
sort (GNU coreutils) 8.4
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Mike Haertel and Paul Eggert.
答案 0 :(得分:4)
您需要仅为第二个字段设置密钥,即-k2,2
,否则如果您使用-k2
,则从第二个字段开始的所有字段都将被视为密钥。
工作一:
$ sort -t, -k2,2n file.txt
207.241.237.163,2226,1854
66.199.199.221,115645,0
207.233.77.147,120167,0
204.38.48.1,125767,0
83.144.97.50,127944,3
12.174.177.15,134080,0
195.76.177.90,138124,0
50.202.17.163,162618,0
66.64.209.30,163729,0
40.76.63.140,181976,0
非工作人员:
$ sort -t, -k2n file.txt
66.199.199.221,115645,0
207.233.77.147,120167,0
204.38.48.1,125767,0
83.144.97.50,127944,3
12.174.177.15,134080,0
195.76.177.90,138124,0
50.202.17.163,162618,0
66.64.209.30,163729,0
40.76.63.140,181976,0
207.241.237.163,2226,1854