使用“grep”在文本文件中搜索模式组合的方法是什么?
比如说,我正在寻找“顺便说一下”和其他可能的组合,例如“顺便说一下”和“顺便说一下”
谢谢。
awk 是这个工具,而不是 grep。一行:
awk '/by/ && /the/ && /way/' file
在整个文件中:
gawk -v RS='\0' '/by/ && /the/ && /way/' file
请注意,这是搜索 3 个单词,而不是搜索这 3 个单词之间有空格的组合。那是你要的吗?
如果您需要更多帮助,请提供更多详细信息,包括示例输入和预期输出。
The simplest approach is probably by using regexps. But this is also slightly wrong:
egrep '([ ]*(by|the|way)\>){3}'
What this does is to match on the group of your three words, taking spaces in front of the words
with it (if any) and forcing it to be a complete word (hence the \>
at the end) and matching the string if any of the words in the group occurs three times.
Example of running it:
$ echo -e "the the the\nby the\nby the way\nby the may\nthe way by\nby the thermo\nbypass the thermo" | egrep '([ ]*(by|the|way)\>){3}'
the the the
by the way
the way by
As already said, this procudes a 'false' positive for the the the
but if you can live with that, I'd recommend doing it this way.