如何grep -w可能会或可能不会出现在同一行中的2个单词?

时间:2013-07-08 11:45:23

标签: grep word text-files

我需要两个命令的组合,有没有办法只grep一次?因为文件可能非常大,> 1gb

$ grep -w 'word1' infile
$ grep -w 'word2' infile

我不需要像grep for 2 words existing on the same line这样的行。我只需要避免整个文件的冗余迭代

1 个答案:

答案 0 :(得分:3)

使用它:

grep -E -w "word1|word2" infile

egrep -w "word1|word2" infile

它将匹配与word1,word2或两者匹配的行。


来自man grep:

   -E, --extended-regexp
          Interpret PATTERN as an extended regular expression (ERE, see below).

测试

$ cat file
The fish [ate] the bird.
[This is some] text.
Here is a number [1001] and another [1201].
$ grep -E -w "is|number" file
[This is some] text.
Here is a number [1001] and another [1201].