7

When using fgrep, the man page says it will

Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.

In bash, what's the correct way to insert newlines into the PATTERN argument (i.e. such that it'll match any of the lines according to the man page).

I've tried the following with no luck:

fgrep word1\nword2
fgrep word1\rword2
fgrep "word1\nword2"
fgrep "word1\rword2"

I'd like the command to be all on one line if possible.

2 Answers 2

7

This can be done by using the $'\n' syntax (see https://stackoverflow.com/a/3182519/3822464). So for example:

fgrep word1$'\n'word2

Or you can wrap the whole PATTERN that way (credit don_crissti)

fgrep $'word1\nword2\nword3'
0
4
grep -F 'word1
word2' infile

or, if you prefer on one line:

grep -F -e 'word1' -e 'word2' infile
1
  • Thanks - the -e is a good alternate solution
    – sa289
    Commented Jan 13, 2016 at 23:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .