Afshin Afshin - 10 months ago 37
Bash Question

grep with regular expression to find two words when word b is AFTER word a in a sentence

I have a big text file, each line containing a sentence.
I want to use grep (or something similar in batch) to find sentences where word b occurs exactly or not exactly (some word(s) between them) after word a.

I don't want grep to return a sentence like this:

f g s b d a

because b is not after a but I want to return a sentence like

f g a d m s b f

because b is after a.

It is OK to return sentences where a is both after and before b:

s a s b s a s

I also don't want sentences with only a or b.

I just want the sentences where b is after a (something can be in the middle).

I can easily do it with Python but I want to use the beauty of bash.

Answer Source

Try to do that:

grep "a.*b" file