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
  • 115,346
  • 109
  • 446
  • 738

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
  • 275,237
  • 103
  • 548
  • 598