Interesting Results when using cat with grep vs only grep

A typical situation is one where there is a directory that contains many files. Maybe 100s of files or more. Searching through however many files for a specific string can get annoying, however, here are a couple of ways to do just that. Note that there are many more, but this really isn’t the point of this article. A use case scenario is an Apache web server with a few dozen php files, and the string of interest is something like “Current Leaders”.

One way to do this is to use cat and pipe the results through grep, like this.

cat *.php | grep "Current Leaders"

The results are accurate, however, leaves the question, which file or files contain these strings?

        echo ' <b>Current Leaders (# wins):</b><br />' . "\n";
echo ' <b>Current Leaders (pick %):</b><br />' . "\n";

A more direct approach is using grep without cat

grep "Current Leaders" *.php

The results not only include the strings, however, also includes the filename.

column_right.php:       echo ' <b>Current Leaders (# wins):</b><br />' . "\n";
column_right.php:       echo ' <b>Current Leaders (pick %):</b><br />' . "\n";