1

I would need the combination of the 2 commands, is there a way to just grep once? Because the file may be really big, >1gb

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

I don't need them on the same line like grep for 2 words existing on the same line. I just need to avoid redundant iteration of the whole file

Community
  • 1
  • 1
alvas
  • 105,505
  • 99
  • 405
  • 683

1 Answers1

3

use this:

grep -E -w "word1|word2" infile

or

egrep -w "word1|word2" infile

It will match lines matching either word1, word2 or both.


From man grep:

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

Test

$ 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].
fedorqui
  • 252,262
  • 96
  • 511
  • 570