Wednesday, July 11, 2018

regex - Match two strings in one line with grep




I am trying to use grep to match lines that contain two different strings. I have tried the following but this matches lines that contain either string1 or string2 which not what I want.



grep 'string1\|string2' filename


So how do I match with grep only the lines that contain both strings?


Answer



You can use grep 'string1' filename | grep 'string2'




Or, grep 'string1.*string2\|string2.*string1' filename


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...