Ubuntu – Grep searching two words in a line

greptext processing

I've been trying to find a way to filter a line that has the word "lemon" and "rice" in it. I know how to find "lemon" or "rice" but not the two of them. They don't need to be next to the other, just one the same line of text.

Best Answer

"Both on the same line" means "'rice' followed by random characters followed by 'lemon' or the other way around".

In regex that is rice.*lemon or lemon.*rice. You can combine that using a |:

grep -E 'rice.*lemon|lemon.*rice' some_file

If you want to use normal regex instead of extended ones (-E) you need a backslash before the |:

grep 'rice.*lemon\|lemon.*rice' some_file

For more words that quickly gets a bit lengthy and it's usually easier to use multiple calls of grep, for example:

grep rice some_file | grep lemon | grep chicken