I want to search a webserver running unix for php-files containing a specific string. Usually I use these commands to accomplish this:

find . -name "*.php" -print0 | xargs -0 grep -H -i "the string to search for"

This has worked great so far, but now I've encountered a server where all the php-scripts don't have any line feeds, but instead only carriage returns. grep doesn't seem to recognize carriage return as new line, so the command above will print the entire contents of a file if there is a match within it, instead of just printing the line.

Any help would be greatly appreciated!

closed as too localized by Michael Mrozek Sep 30 '11 at 21:43

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.


Not very efficient, but should work:

find . -name '*.php' -exec bash -c "grep 'pattern' <(tr '\r' '\n' <{})" \;

This an alternative to show the file name:

find . -name '*.php' -exec bash -c "grep 'pattern' <(tr '\r' '\n' <'{}') |
    sed s+^+'{}':\ +" \;
  • I really need the location of the file foreach match as well. Otherwise it was perfect. – quano Oct 1 '11 at 11:50
  • @quano: here it is – enzotib Oct 1 '11 at 12:11
  • wait, does this stop to search once it finds one match? That's what it seems like. – quano Oct 2 '11 at 13:08
  • @quano: it should not – enzotib Oct 2 '11 at 16:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.