Using grep, awk and sed to list files containing a string

This week I was trying to locate which files in the codebase use a specific class method while debugging some code. This method however has been used a lot, when I say a lot I mean 10 to 20 times per file, and in lots of templates. This is mainly because it is a template helper to generate complex links within the page. Anyway, a simple use of grep wasn’t all that much help, so I refreshed my memory on how to use sed to make the output a little more readable.

The first command I used was simply to grep for all matches

grep -ri link_to *

This of course gave back some very noisy output

articles/templates/showSuccess.php:		<?php echo link_to('Tweet', 'http://twitter.com/share', array(
articles/templates/_list.php:				<?php echo link_to(cached_thumbnail_image_tag($article->getFirstImage(), 100, 100), 'articleShow', $article) ?>
articles/templates/_list.php:				<?php echo link_to(image_tag('article_thumbnail_100x100'), 'articleShow', $article) ?>
articles/templates/_list.php:			<h3><?php echo link_to($article->getHeadline(), 'articleShow', $article) ?></h3>
articles/templates/_latestGrid.php:					<?php echo link_to(cached_thumbnail_image_tag($article->getFirstImage(), 130, 130), 'articleShow', $article) ?>
articles/templates/_latestGrid.php:					<?php echo link_to(image_tag('article_thumbnail_130x130'), 'articleShow', $article) ?>
articles/templates/_latestGrid.php:			<h3><?php echo link_to($article->getHeadline(), 'articleShow', $article) ?></h3>
articles/templates/_latestGrid.php:					<?php echo link_to(cached_thumbnail_image_tag($article->getFirstImage(), 50, 50), 'articleShow', $article) ?>
articles/templates/_latestGrid.php:					<?php echo link_to(image_tag('article_thumbnail_50x50'), 'articleShow', $article) ?>
articles/templates/_latestGrid.php:			<h3><?php echo link_to($article->getHeadline(), 'articleShow', $article) ?></h3>
generic/templates/error404Success.php:		<li><?php echo link_to('Home', 'homepage') ?gt;</li>
generic/templates/error404Success.php:		<li><?php echo link_to('Music', '@articlesList?category_slug=music') ?gt;</li>
generic/templates/error404Success.php:		<li><?php echo link_to('Scene', '@articlesList?category_slug=scene') ?gt;</li>
generic/templates/error404Success.php:		<li><?php echo link_to('News & Ents', '@articlesList?category_slug=news-and-ents') ?gt;</li>
generic/templates/error404Success.php:		<li><?php echo link_to('Features', '@articlesList?category_slug=features') ?gt;</li>
generic/templates/_headerNavigation.php:		<li><?php echo link_to($item['text'], $item['route'], $item['params']) ?gt;</li>

To clean that up a little we can use the awk command to only display the filenames, and while we’re at it we can only list the unique ones, so push that through sort and uniq

 grep -ri link_to * | awk '{print $1}' | sort | uniq

The output of these changes gives the slightly nice listing

articles/templates/_latestGrid.php:
articles/templates/_list.php:
articles/templates/showSuccess.php:
generic/templates/error404Success.php:
generic/templates/_headerNavigation.php:

Now you might say that is it, but what about sed? I’ve not used that yet, and I only intend to use it so that I can pipe that list of files into vim for editing. The extra colon on the end will parse the wrong filename into the editor so we need to strip it out first. To do this we need to add a sed call into our command

 grep -ri link_to * | awk '{print $1}' | sort | uniq | sed 's/://g'

There you have it, a nice way to locate all the files containing a given string and listing the filenames in a way they can be pushed into something else. Oh, and just in case you want to open them all in vim like I did, the command was:

 vim -o `grep -ri link_to * | awk '{print $1}' | sort | uniq | sed 's/://g' `

Leave a Reply

Your email address will not be published. Required fields are marked *