来自这问题:
在从以下文件中搜索字符串“banana”时,我们希望分别找到第 1、2、3 和 4 行的 1、2、3 和 7 个实例。grep 输出的数量应等于匹配实例的数量,同时仍返回整行。
There is one banana here
There are two banana banana here
There are three banana banana banana here
Basically there is no limit to how many banana banana banana banana banana banana banana we can have
In fact we need not have any too!
注意:如果我们删除输出中整行的限制,我们会得到:
grep -no "banana" tempfile
返回
1:banana
2:banana
2:banana
3:banana
3:banana
3:banana
4:banana
4:banana
4:banana
4:banana
4:banana
4:banana
4:banana
有任何想法吗?
编辑:这是预期的输出
1 There is one banana here
2 There are two banana banana here
2 There are two banana banana here
3 There are three banana banana banana here
3 There are three banana banana banana here
3 There are three banana banana banana here
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
答案1
使用perl
字符串重复运算符,通过在标量上下文中评估匹配结果来获取重复次数:
$ perl -pe '$_ x= (() = /banana/g)' file
There is one banana here
There are two banana banana here
There are two banana banana here
There are three banana banana banana here
There are three banana banana banana here
There are three banana banana banana here
Basically there is no limit to how many banana banana banana banana banana banana banana we can have
Basically there is no limit to how many banana banana banana banana banana banana banana we can have
Basically there is no limit to how many banana banana banana banana banana banana banana we can have
Basically there is no limit to how many banana banana banana banana banana banana banana we can have
Basically there is no limit to how many banana banana banana banana banana banana banana we can have
Basically there is no limit to how many banana banana banana banana banana banana banana we can have
Basically there is no limit to how many banana banana banana banana banana banana banana we can have
答案2
grep
没有匹配计数器,只有-c
匹配行数的计数器,但我们可以使用awk
它来做到这一点。据我所知,您希望根据匹配数量打印匹配 x 次的行。好吧,它在这里:
$ awk '{for(i=1;i<=NF;i++) if($i=="banana") counter++;for(j=1;j<=counter;j++) print NR,$0;counter=0 }' input.txt
1 There is one banana here
2 There are two banana banana here
2 There are two banana banana here
3 There are three banana banana banana here
3 There are three banana banana banana here
3 There are three banana banana banana here
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
4 Basically there is no limit to how many banana banana banana banana banana banana banana we can have
这里的基本思想是循环遍历一行中的每个单词,并计算匹配项。如果匹配,则增加计数器,然后使用该计数器循环打印同一行。最后重置计数器并重复此过程
答案3
它不是很漂亮,但你可以使用类似的东西
awk '{print NR, gsub(/banana/, "")' <tempfile
它的工作原理是使用awk
gsub
命令替换匹配模式(这里是香蕉),并返回执行替换的次数。如果是,则输出输入行号和匹配数
或者多次重复输入行
awk '{A=$0
b=gsub(/banana/, "")
for (i=1; i<=b; i++) print A
}' <tempfile