sfa*_*tor 17 linux bash shell command-line
我一直在尝试在bash中的制表符分隔文件的每一列中获取唯一值.所以,我使用了以下命令.
cut -f <column_number> <filename> | sort | uniq -c
Run Code Online (Sandbox Code Playgroud)
它工作正常,我可以在列中获取唯一值,它的计数就像
105 Linux
55 MacOS
500 Windows
Run Code Online (Sandbox Code Playgroud)
我想要做的不是按列值名称(在本例中是操作系统名称)进行排序,我想按计数对它们进行排序,并且可能在此输出格式的第二列中有计数.所以它必须看起来像:
Windows 500
MacOS 105
Linux 55
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
pax*_*blo 18
使用:
cut -f <col_num> <filename>
| sort
| uniq -c
| sort -r -k1 -n
| awk '{print $2" "$1}'
Run Code Online (Sandbox Code Playgroud)
的sort -r -k1 -n种类以相反的顺序,使用第一字段为数值.在awk简单地反转列的顺序.您可以测试添加的管道命令(具有更好的格式):
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
Run Code Online (Sandbox Code Playgroud)