I am trying to accomplish the following result with bash:
grep
files for some substring.- take the filenames (which can contain spaces) for these files and print them.
- after printing each filename I want to grep again, however this time inside that file only.
So the result looks like this:
/tmp/file1.txt
it works!
/tmp/file2.txt
/tmp/file3.txt
it works!
I tried constructing a command like this:
grep -irl "something" . | xargs -I % sh -c 'echo "%"; cat "%" | grep -i "another thing" "%"'
Explanation: grep -l
returns the filenames. Then I print those filenames with echo "%"
and then I want to grep inside the file.
But no luck. What is the lesson here?
To clarify: My actual problem/goal is to find all files that contain a string A but do not contain a string B as well.
More clarification: I want something like grep A | grep -v B
but I want a list of all files that matched A
, as well.
grep
output toxargs
.grep
?