2

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?

agc
  • 7,973
  • 2
  • 29
  • 50
jwbensley
  • 10,534
  • 19
  • 75
  • 93
  • I'm only guessing here, but try to encompass your expression with a pair of parenthesis (`"expression"`), or delimiters (`/expression/`) – Madara's Ghost Apr 11 '12 at 16:30
  • 1
    Possible duplicate of [How to use grep to match string1 AND string2?](https://stackoverflow.com/q/4487328/608639) – jww Apr 27 '19 at 20:55

2 Answers2

5

Try this:

grep ".*quick.*the cat" myfile
lamelas
  • 872
  • 6
  • 15
4

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

grep ".*quick.*the cat" myfile
cheeze
  • 427
  • 5
  • 16
  • Oh man, I wouldn't have posted this question without trying that, as it's so blindingly obvious, and I'm sure I did. I must have made a typo, still thanks anyway for pointing out I'm an idiot :) – jwbensley Apr 12 '12 at 07:56
  • No prob :) I've been through that myself - the combinations of the quotes plus the regex confuses even the experienced. – cheeze Aug 08 '12 at 10:50