<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" > <channel><title>Comments on: recursively grep only certain files</title> <atom:link href="http://adammonsen.com/post/301/feed" rel="self" type="application/rss+xml" /><link>http://adammonsen.com/post/301?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=recursively-grep-only-certain-files</link> <description></description> <lastBuildDate>Sat, 31 Mar 2012 19:34:03 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.2</generator> <item><title>By: adam</title><link>http://adammonsen.com/post/301/comment-page-1#comment-11479</link> <dc:creator>adam</dc:creator> <pubDate>Sun, 30 Sep 2007 22:45:21 +0000</pubDate> <guid isPermaLink="false">http://adammonsen.com/post/301#comment-11479</guid> <description>While I&#039;m at it, here&#039;s one way to recurse down selected directory paths, searching within files for an arbitrary pattern: &lt;pre lang=&quot;bash&quot;&gt; #!/bin/bash find . -path &#039;*.svn*&#039; -prune -o -type f -print0 &#124; xargs -0 grep --col &quot;$1&quot; &lt;/pre&gt; This is of course a bash script wherein the first argument is the pattern to search for within the files. I&#039;ve hardcoded it to skip any files including &lt;code&gt;&#039;.svn&#039;&lt;/code&gt; in their full path. I guess I&#039;ve also hardcoded where to start the search (the current directory).</description> <content:encoded><![CDATA[<p>While I&#8217;m at it, here&#8217;s one way to recurse down selected directory paths, searching within files for an arbitrary pattern:</p><div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #c20cb9; font-weight: bold;">find</span> . <span style="color: #660033;">-path</span> <span style="color: #ff0000;">'*.svn*'</span> <span style="color: #660033;">-prune</span> <span style="color: #660033;">-o</span> <span style="color: #660033;">-type</span> f <span style="color: #660033;">-print0</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #660033;">-0</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">--col</span> <span style="color: #ff0000;">&quot;$1&quot;</span></pre></div></div><p>This is of course a bash script wherein the first argument is the pattern to search for within the files. I&#8217;ve hardcoded it to skip any files including <code>'.svn'</code> in their full path. I guess I&#8217;ve also hardcoded where to start the search (the current directory).</p> ]]></content:encoded> </item> <item><title>By: adam</title><link>http://adammonsen.com/post/301/comment-page-1#comment-11476</link> <dc:creator>adam</dc:creator> <pubDate>Wed, 26 Sep 2007 15:44:48 +0000</pubDate> <guid isPermaLink="false">http://adammonsen.com/post/301#comment-11476</guid> <description>The first set of &lt;code&gt;egrep&lt;/code&gt;s you provide are fine if you know the exact directory depth of the source files.&lt;code&gt;egrep&lt;/code&gt; regex syntax is fine, I&#039;m just more used to Perl-style regexes. The &lt;code&gt;-H&lt;/code&gt; is necessary if find only passes one filename to xargs, but you&#039;re right, it would be fine to omit that.I agree the &lt;code&gt;grep&lt;/code&gt; args &lt;code&gt;-r&lt;/code&gt; paired with &lt;code&gt;--include&lt;/code&gt; are conceptually the same as using find and piping to &lt;code&gt;xargs&lt;/code&gt; and &lt;code&gt;grep&lt;/code&gt;, but you&#039;re firing up three processes when only one is necessary. Also, the command line is a little shorter. :)</description> <content:encoded><![CDATA[<p>The first set of <code>egrep</code>s you provide are fine if you know the exact directory depth of the source files.</p><p><code>egrep</code> regex syntax is fine, I&#8217;m just more used to Perl-style regexes. The <code>-H</code> is necessary if find only passes one filename to xargs, but you&#8217;re right, it would be fine to omit that.</p><p>I agree the <code>grep</code> args <code>-r</code> paired with <code>--include</code> are conceptually the same as using find and piping to <code>xargs</code> and <code>grep</code>, but you&#8217;re firing up three processes when only one is necessary. Also, the command line is a little shorter. :)</p> ]]></content:encoded> </item> <item><title>By: jason</title><link>http://adammonsen.com/post/301/comment-page-1#comment-11473</link> <dc:creator>jason</dc:creator> <pubDate>Wed, 26 Sep 2007 05:52:48 +0000</pubDate> <guid isPermaLink="false">http://adammonsen.com/post/301#comment-11473</guid> <description>what about: &lt;pre lang=&quot;bash&quot;&gt; egrep &#039;^#include +&lt;rpc&#039; */*.c egrep &#039;^#include +&lt;rpc&#039; */*/*.c egrep &#039;^#include +&lt;rpc&#039; */*/*/*.c egrep &#039;^#include +&lt;rpc&#039; */*/*/*/*.c &lt;/pre&gt;If you&#039;re in a hurry the command line substitution is pretty quick to add another splat slash to each one until you run out of directory depth.egrep has better support for regexes than plain &#039;ol grep does.&lt;pre lang=&quot;bash&quot;&gt; find . -type f -name *.c -print0 &#124;xargs -0 egrep &#039;^#include +&lt;rpc&#039; &lt;/pre&gt;I don&#039;t think you need the -P if you use egrep or the -H at all. I&#039;m not sure what you&#039;re getting out of the -r as find is passing xargs fully qualified paths to files that you&#039;re searching. You can&#039;t descend to a subdirectory of a file.Maybe I&#039;m missing something. etc...</description> <content:encoded><![CDATA[<p>what about:</p><div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #ff0000;">'^#include +&lt;rpc'</span> <span style="color: #000000; font-weight: bold;">*/*</span>.c
<span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #ff0000;">'^#include +&lt;rpc'</span> <span style="color: #000000; font-weight: bold;">*/*/*</span>.c
<span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #ff0000;">'^#include +&lt;rpc'</span> <span style="color: #000000; font-weight: bold;">*/*/*/*</span>.c
<span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #ff0000;">'^#include +&lt;rpc'</span> <span style="color: #000000; font-weight: bold;">*/*/*/*/*</span>.c</pre></div></div><p>If you&#8217;re in a hurry the command line substitution is pretty quick to add another splat slash to each one until you run out of directory depth.</p><p>egrep has better support for regexes than plain &#8216;ol grep does.</p><div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> . <span style="color: #660033;">-type</span> f <span style="color: #660033;">-name</span> <span style="color: #000000; font-weight: bold;">*</span>.c <span style="color: #660033;">-print0</span> <span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #660033;">-0</span> <span style="color: #c20cb9; font-weight: bold;">egrep</span> <span style="color: #ff0000;">'^#include +&lt;rpc'</span></pre></div></div><p>I don&#8217;t think you need the -P if you use egrep or the -H at all. I&#8217;m not sure what you&#8217;re getting out of the -r as find is passing xargs fully qualified paths to files that you&#8217;re searching. You can&#8217;t descend to a subdirectory of a file.</p><p>Maybe I&#8217;m missing something.<br /> etc&#8230;</p> ]]></content:encoded> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced

Served from: adammonsen.com @ 2012-05-17 13:26:38 -->
