1

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

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

对于存在于同一行的 2 个单词,我不需要它们像grep 一样位于同一行。我只需要避免整个文件的冗余迭代

4

1 回答 1

3

用这个:

grep -E -w "word1|word2" infile

或者

egrep -w "word1|word2" infile

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


从人 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].
于 2013-07-08T11:47:13.693 回答