I need to check if two (specified) words exist on any line in a text file. There are no limits for the characters of the words. For example:
I want to find lines of a text file that contain the two words “cat” and “elephant” together (i.e., on the same line; not necessarily side-by-side):
Cat is smaller than elephant
Elephant is larger than cat
Cats are cute!
Elephants are very strong
Cat and elephants live in different environments
cats are friendly
In the previous examples, how can I find the lines containing both words?
Cat is smaller than elephant
Elephant is larger than cat
Cat and elephants live in different environments
I tried grep and awk with no hope. The problem is there are words that have upper and lower case, so how can I match for both words regardless of their letter status!?
grep
again, but usegrep -i
. This makes its matching disregard the case of the letters. Also, please show what you've tried so that other's make comment and give suggestions for improvement. For example, does your command distinguish that "catnip" is not the word "cat"? – Kusalananda Oct 16 '18 at 22:21