Thursday, 7 July 2016

Grep with two ordered strings (like logical AND in regex)





I am trying to grep a file for lines with two strings, so string1 logical_AND string2.



Example line to grep for:



The quick brown fox and the cat



I want to grep out that line by matching for "quick" and "the cat", note the space between "the" and "cat". That is what is throwing me off.



grep .*quick.*cat myfile


This works fine but I want to grep for "the cat" and;



grep .*quick.*the cat myfile



Obviously doesn't work because of the space. I don't understand how I can encompass "the cat" with the space in it?


Answer



Try putting quotes around the string you are searching for i.e.



grep ".*quick.*the cat" myfile

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...