I am trying to search for a "pattern 1" only in files that contain another string "pattern 2"
E.g.
A.txt Pattern 1 ... Pattern 2
B.txt Pattern Z ... Pattern 2
I intend to filter out files that match Pattern 1 in this case A.txt and then search occurrences of Pattern 2 only in A.txt.
I am trying below but doesn't work.
grep -rl "Pattern 1" . -exec grep -or "Pattern 2" +
Note: Some file paths include spaces.
find
variant, you can use-exec grep -l 'Pattern 2' {} +
(and remove-print
) to avoid some of the execs. – Kusalananda Jul 15 '19 at 11:00grep
separate the files already ? – pLumo Jul 15 '19 at 11:06grep
has to be run as an individual test on each file. The second one can run on all that passes the first test in one go though. – Kusalananda Jul 15 '19 at 11:08