匹配线与图案n次在同一行 [英] Match lines with pattern n times in the same line

查看:110
本文介绍了匹配线与图案n次在同一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,我需要过滤模式的有(或没有)第N发生。
也就是说,如果我的模式是信 0 ,我什么以匹配信 0 恰好出现4次线,恩pression应与其他第一下面的示例中的行而不是:

I have a file and I need to filter lines that have (or don't have) N occurrences of a pattern. I.e., if my pattern is the letter o and I what to match lines where the letter o occurs exactly 4 times, the expression should match the first of the following example lines but not the others:

foo foo  
foo  
foo foo foo   

我thouth我可以在vim正则表达式做到这一点,还是用sed,awk中,或任何其他工具。
我GOOGLE了,并没有发现任何人,也做了类似的事情。
将可能有做一个脚本或类似分析每一行的东西。
有没有人做过类似的事情?

I thouth I could do it with a regex in vim, or sed, awk, or any other tool. I've googled and haven't found anyone that has done a similar thing. Probably will have do a script or something similar to parse each line. Does anyone have done a similar thing?

感谢

推荐答案

一个Perl的单行:

perl -ne 'print if(tr/o/o/ == 4)' foo_file

这篇关于匹配线与图案n次在同一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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