This will find any php file containing "the string to search for", and print the file name and the line in which the match was made.

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.

Unfortunately, grep won't do what you want. There isn't a command line option to get it to recognize the CR character as a line separator. However, you can do what you want with a bit of awk instead! Try this:

Awk isn't nearly as fast as grep, so this method could take a lot longer depending on the number of files and their sizes. It may be worthwhile to simply convert all of the line endings of your PHP files if you're going to do a lot of grepping on them. If you don't have a convenient utility available to do this for you, this shell script ought to do it:

So, I'm only getting the filename. Was that what you intended? I guess it's better than what my line does but it would be great to see the actual match (line) of each file as well.
–
quanoSep 30 '11 at 20:48