Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何使用Unix排序命令按列中人类可读的数字文件大小进行排序?_Linux_Sorting_Unix - Fatal编程技术网

Linux 如何使用Unix排序命令按列中人类可读的数字文件大小进行排序?

Linux 如何使用Unix排序命令按列中人类可读的数字文件大小进行排序?,linux,sorting,unix,Linux,Sorting,Unix,这个问题现在已经回答了-滚动到本帖的末尾以获得解决方案 如果答案已经在这里,我表示歉意,但是到目前为止我找到的所有答案都建议使用-h标志或-n标志,而这两个标志都不适用于我 我有一个curl命令的一些输出,它给了我几列数据。其中一列是人类可读的文件大小(“1.6mb”、“4.3gb”等) 我使用unixsort命令按相关列排序,但它似乎试图按字母顺序而不是数字顺序排序。我尝试过同时使用-n和-h标志,但尽管它们确实改变了顺序,但在这两种情况下,顺序在数字上都不正确 我在CentOS Linux

这个问题现在已经回答了-滚动到本帖的末尾以获得解决方案

如果答案已经在这里,我表示歉意,但是到目前为止我找到的所有答案都建议使用-h标志或-n标志,而这两个标志都不适用于我

我有一个curl命令的一些输出,它给了我几列数据。其中一列是人类可读的文件大小(“1.6mb”、“4.3gb”等)

我使用unixsort命令按相关列排序,但它似乎试图按字母顺序而不是数字顺序排序。我尝试过同时使用-n和-h标志,但尽管它们确实改变了顺序,但在这两种情况下,顺序在数字上都不正确

我在CentOS Linux box上,版本7.2.1511。我的sort版本是“sort(GNU-coreutils)8.22”

我已尝试在以下不同格式中使用-h标志:

curl localhost:9200/_cat/indices | sort -k9,9h | head -n5
curl localhost:9200/_cat/indices | sort -k9 -h | head -n5
curl localhost:9200/_cat/indices | sort -k 9 -h | head -n5
curl localhost:9200/_cat/indices | sort -k9h | head -n5
我总是得到这些结果:

green open indexA            5 1        0       0   1.5kb    800b
green open indexB            5 1  9823178 2268791 152.9gb  76.4gb
green open indexC            5 1    35998    7106 364.9mb 182.4mb
green open indexD            5 1      108      11 387.1kb 193.5kb
green open indexE            5 1        0       0   1.5kb    800b
green open index1      5 1     1021       0   3.2mb   1.6mb
green open index2      5 1     8833       0   4.1mb     2mb
green open index3      5 1     4500       0     5mb   2.5mb
green open index4      1 0        3       0   3.9kb   3.9kb
green open index5      3 1  2516794       0   8.6gb   4.3gb
我已尝试以与上面相同的格式使用-n标志:

curl localhost:9200/_cat/indices | sort -k9,9n | head -n5
curl localhost:9200/_cat/indices | sort -k9 -n | head -n5
curl localhost:9200/_cat/indices | sort -k 9 -n | head -n5
curl localhost:9200/_cat/indices | sort -k9n | head -n5
我总是得到这些结果:

green open indexA            5 1        0       0   1.5kb    800b
green open indexB            5 1  9823178 2268791 152.9gb  76.4gb
green open indexC            5 1    35998    7106 364.9mb 182.4mb
green open indexD            5 1      108      11 387.1kb 193.5kb
green open indexE            5 1        0       0   1.5kb    800b
green open index1      5 1     1021       0   3.2mb   1.6mb
green open index2      5 1     8833       0   4.1mb     2mb
green open index3      5 1     4500       0     5mb   2.5mb
green open index4      1 0        3       0   3.9kb   3.9kb
green open index5      3 1  2516794       0   8.6gb   4.3gb
编辑:结果发现有两个问题:

1) sort希望看到大写的单字母M、K和G,而不是mb、kb和gb(对于字节,可以留空)

2) 除非明确排除前导空格,否则排序将包括前导空格,这会扰乱排序

解决方案是将小写替换为大写,并使用-b标志使排序忽略前导空格(我根据下面的@Vinicius解决方案得出了这个答案,因为如果您不知道正则表达式,它更容易阅读):


你的'm'和'g'单位应该是大写的。内容如下:

-h——人工数字排序——排序=人工数字

按数字排序,首先按数字符号(负、零或正)排序;然后是SI后缀(按该顺序为空或“k”或“k”或“MGTPEZY”中的一个;参见块大小);最后是数值

您可以使用GNU
sed
更改
curl
的输出,如下所示:

curl localhost:9200/_cat/indices \
| sed 's/[0-9][mgtpezy]/\U&/g'
| sort -k9,9h \
| head -n5
收益率:

green open index4      1 0        3       0   3.9kb   3.9kb
green open index1      5 1     1021       0   3.2Mb   1.6Mb
green open index2      5 1     8833       0   4.1Mb     2Mb
green open index3      5 1     4500       0     5Mb   2.5Mb
green open index5      3 1  2516794       0   8.6Gb   4.3Gb
其他字母如“b”将被视为“无单位”:


如果需要,您可以通过管道将排序输出中的单位改回小写,方法是将单位改为
sed/[0-9][MGTPEZY]/\L&/g'
排序
不理解kb、mb和gb。必须使用K、M和G。可以使用
tr
转换后缀:

curl localhost:9200/_cat/indices | tr 'kmgb' 'KMG ' | sort -b -k 9 -h

您是否尝试过使用
curl
上的
--write out size\u download
选项?这几乎奏效了-不完全有效,因为所有数字列都有前导空格,默认情况下排序不会忽略。但是如果你加上-b标志,一切都很好。这样:curl localhost:9200/_cat/index | sed's/[0-9][mgtpezy]/\U&/g'| sort-k9hb | head-n5谢谢!由于空格是分开的字段,所以不应该有任何前导空格问题,我也不清楚为什么必须使用-b选项。也许这取决于你所在的地区?(我用en_US.UTF-8测试了它;如果有任何疑问,请尝试
| LC_ALL=C sort…
)。如果你满意的话,请毫不犹豫地接受我的答案。这几乎奏效了——不完全是因为所有数字列都有前导空格,排序默认情况下不会忽略。但是如果你加上-b标志,一切都很好。像这样:curl localhost:9200/_cat/index | tr'[kmg]b'[kmg]'| sort-k9hb谢谢!在
tr
命令中,
'[kmg]b'
不是正则表达式。它并不意味着“(k或m或g)后跟b”。这意味着用“[”(无用)替换所有“[”,用“k”替换所有“k”,用“m”替换所有“m”,用“g”替换所有“g”,用“]”(无用)替换所有“]”,最后用“b”替换所有“b”。因此,这将影响整行代码,将“green”改为“green”(任何“blue”都改为“lue”)@xhiene你说得对,我非常习惯正则表达式语法,所以我在这里使用它而不需要。我会更新答案。至于绿色变成绿色,如果这是一个问题,解决方案将在您的响应中使用类似regex的代码。我只是试图提供最简单的解决方案。@ClareSudbery我更新了响应以包含-b标志。