使用命令行工具按排序顺序计算重复项 [英] counting duplicates in a sorted sequence using command line tools

查看:23
本文介绍了使用命令行工具按排序顺序计算重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个命令 (cmd1),它通过日志文件 grep 过滤掉一组数字.数字是以随机顺序,所以我使用 sort -gr 来获得一个反向排序的数字列表.里面可能有重复这个排序列表.我需要找到该列表中每个唯一数字的计数.

I have a command (cmd1) that greps through a log file to filter out a set of numbers. The numbers are in random order, so I use sort -gr to get a reverse sorted list of numbers. There may be duplicates within this sorted list. I need to find the count for each unique number in that list.

例如如果 cmd1 的输出是:

For e.g. if the output of cmd1 is:

100 
100 
100 
99 
99 
26 
25 
24 
24

我需要另一个命令,我可以将上面的输出通过管道传输到,这样,我得到:

I need another command that I can pipe the above output to, so that, I get:

100     3
99      2
26      1
25      1
24      2

推荐答案

怎么样;

$ echo "100 100 100 99 99 26 25 24 24" 
    | tr " " "
" 
    | sort 
    | uniq -c 
    | sort -k2nr 
    | awk '{printf("%s	%s
",$2,$1)}END{print}'

结果是:

100 3
99  2
26  1
25  1
24  2

这篇关于使用命令行工具按排序顺序计算重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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