Extract all the sentences that matched two patterns

Hi

I have two lists of patterns named A and B consisting of around 200 entries in each and I want to extract all the sentences from a big text file which match atleast one pattern from both A and B.

For example, pattern list A consists of :

ama
ani
ahum
mari
...
...

and pattern list B consists of :

kok
sam
lai
mit
....
....

The sample input text file looks like below:

This is first sentence with only one pattern ama.
This is second sentence with ahum and kok patterns.
This is the third sentence with only one pattern mit.
This is the fourth sentence consisting of samson and marigold.
This is the fifth sentence with more patterns such as ama, ani, lai and mit.
This is the sixth sentence with no patterns.
..
..

..

The sample output of the script for the given input text file is given below

This is second sentence with ahum and kok patterns.
This is the fourth sentence consisting of samson and marigold.
This is the fifth sentence with more patterns such as ama, ani, lai and mit.

..
..

..

I need help to write a script to extract all the sentences that matches atleast one pattern from each A and B. That is, the output contains atleast one GREEN pattern and one RED pattern for every sentence. Thanks in advance. :slight_smile:

You mean something like:

grep -Ff "File Containing Pattern List A" "sample input text file" | grep -Ff "File Containing Pattern List B"
1 Like