gpt4 book ai didi

bash - 排序命令无法正常工作,错误或功能?

转载 作者:行者123 更新时间:2023-11-29 09:25:45 24 4
gpt4 key购买 nike

使用逗号作为字段分隔符时,排序命令无法正常工作:

文件:

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 --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.

最佳答案

您只需要为第二个字段设置键,即 -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

关于bash - 排序命令无法正常工作,错误或功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32717022/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com