To see all the php files that contain "abc" I can use this simple script:
find . -name "*php" -exec grep -l abc {} \;
I can omit the -l and i get extracted some part of the content instead of the filenames as results:
find . -name "*php" -exec grep abc {} \;
What I would like now is a version that does both at the same time, but on the same line.
Expected output:
path1/filename1: lorem abc ipsum
path2/filename2: ipsum abc lorem
path3/filename3: non abc quod
More or less like grep abc *
does.
Edit: I want to use this as a simple shell script. It would be great if the output is on one line, so further grepping would be possible. But it is not necessary that the script is only one line, i am putting it in a bash script file anyways.
Edit 2: Later I found "ack", which is a great tool and I use this now in most cases instead of grep. It does all this and more. http://betterthangrep.com/ You would write ack --php --nogroup abc
to get the desired result
If you don't need to recursively search, you can just do..
..which gives you the desired output.
-H
is the default behaviour (at least on the OS X version of grep), so you can omit this:You can grep recursively using the
-R
flag, but you're unable limit it to.php
files:Again, this has the same desired output.
I know this doesn't exactly answer your questions, it's just.. an alternative... The above are just grep with a single flag, so are easier to remember than
find
/-exec
/grep
/xargs
combinations! (irrelevant for a script, but useful for day-to-day shell'ing)Use the
-H
switch (man grep
):Alternative using
xargs
(now the-H
switch is not needed, at least for the version ofgrep
I have here):Edit: As a consequence of
grep
's behavior as noted by orsogufo, the second command above should use-H
iffind
could conceivably return only a single filename (i.e. if there is only a single PHP file). If orsogufo's comment w.r.t.-print0
is also incorporated, the command becomes:Edit 2: A (more1) POSIX compliant version as proposed by Jonathan Leffler, which through the use of
/dev/null
avoids the-H
switch:1: A quote from the opengroup.org manual on
find
hints that-print0
is non-standard: