如何grep唯一出现的次数 [英] How to grep number of unique occurrences

查看:20
本文介绍了如何grep唯一出现的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 grep -c string 可用于计算给定字符串的出现次数.我想要做的是在只有部分字符串已知或保持不变时计算唯一出现的次数.

I understand that grep -c string can be used to count the occurrences of a given string. What I would like to do is count the number of unique occurrences when only part of the string is known or remains constant.

例如,如果我有一个文件(在这种情况下是一个日志),其中有几行包含一个常量字符串和一个重复变量,如下所示:

For Example, if I had a file (in this case a log) with several lines containing a constant string and a repeating variable like so:

string=value1
string=value1
string=value1
string=value2
string=value3
string=value2

我希望能够通过类似于以下的输出来识别每个唯一集合的数量:(最好使用单个 grep/awk 字符串)

Than I would like to be able to identify the number of each unique set with an output similar to the following: (ideally with a single grep/awk string)

value1 = 3 occurrences
value2 = 2 occurrences
value3 = 1 occurrences

有没有人有使用 grep 或 awk 的可行解决方案?提前致谢!

Does anyone have a solution using grep or awk that might work? Thanks in advance!

推荐答案

这非常有效...感谢大家的评论!

This worked perfectly... Thanks to everyone for your comments!

grep -oP "wwn=[^,]*" path/to/file |排序 |uniq -c

这篇关于如何grep唯一出现的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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