I want to find files that have "abc" AND "efg" in that order, and those two strings are on different lines in that file. Eg: a file with content:
blah blah..
blah blah..
blah abc blah
blah blah..
blah blah..
blah blah..
blah efg blah blah
blah blah..
blah blah..
Should be matched.
I relied heavily on pcregrep, but with newer grep you do not need to install pcregrep for many of its features. Just use
grep -P
.In the example of the OP's question, I think the following options work nicely, with the second best matching how I understand the question:
I copied the text as /tmp/test1 and deleted the 'g' and saved as /tmp/test2. Here is the output showing that the first shows the matched string and the second shows only the filename (typical -o is to show match and typical -l is to show only filename). Note that the 'z' is necessary for multiline and the '(.|\n)' means to match either 'anything other than newline' or 'newline' - i.e. anything:
To determine if your version is new enough, run
man grep
and see if something similar to this appears near the top:That is from GNU grep 2.10.
I don't know how I would do that with grep, but I would do something like this with awk:
You need to be careful how you do this, though. Do you want the regex to match the substring or the entire word? add \w tags as appropriate. Also, while this strictly conforms to how you stated the example, it doesn't quite work when abc appears a second time after efg. If you want to handle that, add an if as appropriate in the /abc/ case etc.
This should work too?!
$ARGV
contains the name of the current file when reading fromfile_list /s
modifier searches across newline.