开发者

Using Regex to search for several occurrences of a word

How do I search for x or more occurrences of a word using regular expressions and grep in a .txt file开发者_如何学编程 in a linux terminal, for example, find all lines with 4 or more "and"s in Sample.txt.


Try this:

egrep "and(.*?and){3}" data.txt 

And to match "and" regardless of case ("And" or "AND", ...), but skip an "and" that is a part of another word (or name), try:

egrep -i "\band\b(.*?\band\b){3}" data.txt

The -i makes it ignore case, and the word boundaries, \b, will disregard occurrences like "Anand" and "Anderson".


If you need to match and but not bandit, use something like the following:

egrep '\band\b(.+?\band\b){3}' Sample.txt
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜