在bash排序 [英] Sorting in bash

查看:132
本文介绍了在bash排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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?

推荐答案

使用:

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

排序-r -k1 -n 排序以相反的顺序,使用的第一个字段为数值。在 AWK 简单地反转列的顺序。您可以测试从而增加管道命令(有更好的格式化):

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

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

这篇关于在bash排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆