Linux 在 bash 中排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3510275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:22:14  来源:igfitidea点击:

Sorting in bash

linuxbashcommand-lineshell

提问by sfactor

I have been trying to get the unique values in each column of a tab delimited file in bash. So, I used the following command.

我一直试图在 bash 中获取制表符分隔文件的每一列中的唯一值。所以,我使用了以下命令。

cut -f <column_number> <filename> | sort | uniq -c

It works fine and I can get the unique values in a column and its count like

它工作正常,我可以获得列中的唯一值及其计数

105 Linux
55  MacOS
500 Windows

What I want to do is instead of sorting by the column value names (which in this example are OS names) I want to sort them by count and possibly have the count in the second column in this output format. So It will have to look like:

我想要做的是,不是按列值名称(在本例中是操作系统名称)排序,我想按计数对它们进行排序,并且可能在此输出格式的第二列中包含计数。所以它必须看起来像:

Windows 500
MacOS   105
Linux   55

How do I do this?

我该怎么做呢?

采纳答案by paxdiablo

Use:

用:

cut -f <col_num> <filename>
    | sort 
    | uniq -c
    | sort -r -k1 -n
    | awk '{print " "}'

The sort -r -k1 -nsorts in reverse order, using the first field as a numeric value. The awksimply reverses the order of the columns. You can test the added pipeline commands thus (with nicer formatting):

所述sort -r -k1 -n排序以相反的顺序,使用第一字段为数值。在awk简单地反转列的顺序。您可以因此测试添加的管道命令(使用更好的格式):

pax> echo '105 Linux
55  MacOS
500 Windows' | sort -r -k1 -n | awk '{printf "%-10s %5d\n",,}'
Windows      500
Linux        105
MacOS         55

回答by sourcerebels

Mine:

矿:

cut -f <column_number> <filename> | sort | uniq -c | awk '{ print " "}' | sort

This will alter the column order (awk) and then just sort the output.

这将改变列顺序 (awk),然后对输出进行排序。

Hope this will help you

希望能帮到你

回答by kartik trivikram

Using sed based on Tagged RE:

使用基于 Tagged RE 的 sed:

cut -f <column_number> <filename> | sort | uniq -c | sort -r -k1 -n | sed 's/\([0-9]*\)[ ]*\(.*\)/ /'

Doesn't produce output in a neat format though.

虽然不会以整洁的格式产生输出。