Grep searching two words in a line
"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
You can pipe the output of first grep command to another grep command and that would match both the patterns. So, you can do something like:
grep <first_pattern> <file_name> | grep <second_pattern>
or,
cat <file_name> | grep <first_pattern> | grep <second_pattern>
Example:
Let's add some contents to our file:
$ echo "This line contains lemon." > test_grep.txt
$ echo "This line contains rice." >> test_grep.txt
$ echo "This line contains both lemon and rice." >> test_grep.txt
$ echo "This line doesn't contain any of them." >> test_grep.txt
$ echo "This line also contains both rice and lemon." >> test_grep.txt
What does the file contain:
$ cat test_grep.txt
This line contains lemon.
This line contains rice.
This line contains both lemon and rice.
This line doesn't contain any of them.
This line also contains both rice and lemon.
Now, let's grep what we want:
$ grep rice test_grep.txt | grep lemon
This line contains both lemon and rice.
This line also contains both rice and lemon.
We only get the lines where both the patterns match. You can extend this and pipe the output to another grep command for further "AND" matches.
Though the question asks for 'grep', I thought it might be helpful to post a simple 'awk' solution:
awk '/lemon/ && /rice/'
This can easily be extended with more words, or other boolean expressions besides 'and'.
Another idea to finding the matches in any order is using:
grep with -P
(Perl-Compatibility) option and positive lookahead regex (?=(regex))
:
grep -P '(?=.*?lemon)(?=.*?rice)' infile
or you can use below, instead:
grep -P '(?=.*?rice)(?=.*?lemon)' infile
- The
.*?
means matching any characters.
that occurrences zero or more times*
while they are optional followed by a pattern(rice
orlemon
). The?
makes everything optional before it (means zero or one time of everything matched.*
)
(?=pattern)
: Positive Lookahead: The positive lookahead construct is a pair of parentheses, with the opening parenthesis followed by a question mark and an equals sign.
So this will return all lines with contains both lemon
and rice
in random order. Also this will avoid of using |
s and doubled grep
s.
External links:
Advanced Grep Topics
Positive Lookahead – GREP for Designers
grep -e foo -e goo
Will return matches for either foo or goo