Write a shell script that find-greps and outputs filename and content in 1 line
bash, find, grep, shell
Solution
Use the `-H` switch (`man grep`):
find . -name "*php" -exec grep -H abc {} \;
Alternative using `xargs` (now the `-H` switch is not needed, at least for the version of `grep` I have here):
find . -name "*php" -print | xargs grep abc
Edit: As a consequence of `grep`'s behavior as noted by orsogufo, the second command above should use `-H` if `find` 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:
find . -name "*php" -print0 | xargs -0 grep -H abc
Edit 2: A (more1) POSIX compliant version as proposed by Jonathan Leffler, which through the use of `/dev/null` avoids the `-H` switch:
find . -name "*php" -print0 | xargs -0 grep abc /dev/null
1: A quote from the opengroup.org manual on `find` hints that `-print0` is non-standard:
A feature of SVR4's find utility was the -exec primary's + terminator. This allowed filenames containing special characters (especially s) to be grouped together without the problems that occur if such filenames are piped to xargs. Other implementations have added other ways to get around this problem, notably a -print0 primary that wrote filenames with a null byte terminator. This was considered here, but not adopted. Using a null terminator meant that any utility that was going to process find's -print0 output had to add a new option to parse the null terminators it would now be reading.
Problem
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