<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Tools · QuickRef</title><link>https://qref.eknath.dev/tags/tools/</link><description>Tools updates from QuickRef</description><language>en-us</language><atom:link href="https://qref.eknath.dev/tags/tools/index.xml" rel="self" type="application/rss+xml"/><lastBuildDate>Sun, 01 Jan 2023 00:00:00 +0000</lastBuildDate><item><title>Grep</title><link>https://qref.eknath.dev/tools/grep/</link><guid isPermaLink="true">https://qref.eknath.dev/tools/grep/</guid><pubDate>Sun, 01 Jan 2023 00:00:00 +0000</pubDate><category>tools</category><category>linux</category><category>cli</category><category>text</category><category>search</category><description>Text search and pattern matching utility for searching plain-text data sets for lines that match a regular expression.</description><content:encoded><![CDATA[<h2 id="overview">Overview</h2>
<p>Grep is a command-line utility for searching plain-text data sets for lines that match a regular expression. It&rsquo;s one of the most powerful and frequently used tools in Unix/Linux systems.</p>
<h2 id="basic-usage">Basic Usage</h2>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>grep <span style="color:#a5d6ff">&#34;pattern&#34;</span> filename
</span></span></code></pre></div><p><strong>Example:</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Search for the word &#34;error&#34; in log file</span>
</span></span><span style="display:flex;"><span>grep <span style="color:#a5d6ff">&#34;error&#34;</span> /var/log/system.log
</span></span></code></pre></div><h2 id="common-options">Common Options</h2>
<h3 id="case--matching">Case &amp; Matching</h3>
<ul>
<li><code>-i</code> - Ignore case</li>
<li><code>-v</code> - Invert match (exclude)</li>
<li><code>-w</code> - Match whole words</li>
<li><code>-x</code> - Match whole lines</li>
<li><code>-F</code> - Fixed strings (no regex)</li>
<li><code>-E</code> - Extended regex</li>
</ul>
<h3 id="output-control">Output Control</h3>
<ul>
<li><code>-n</code> - Show line numbers</li>
<li><code>-c</code> - Count matches</li>
<li><code>-l</code> - List matching files</li>
<li><code>-L</code> - List non-matching files</li>
<li><code>-o</code> - Show only matches</li>
<li><code>-q</code> - Quiet mode</li>
</ul>
<h2 id="context-options">Context Options</h2>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Show 3 lines after match</span>
</span></span><span style="display:flex;"><span>grep -A <span style="color:#a5d6ff">3</span> <span style="color:#a5d6ff">&#34;pattern&#34;</span> file
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Show 3 lines before match</span>
</span></span><span style="display:flex;"><span>grep -B <span style="color:#a5d6ff">3</span> <span style="color:#a5d6ff">&#34;pattern&#34;</span> file
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Show 3 lines around match</span>
</span></span><span style="display:flex;"><span>grep -C <span style="color:#a5d6ff">3</span> <span style="color:#a5d6ff">&#34;pattern&#34;</span> file
</span></span></code></pre></div><h2 id="regular-expressions">Regular Expressions</h2>
<h3 id="basic-patterns">Basic Patterns</h3>
<ul>
<li><code>^</code> - Start of line</li>
<li><code>$</code> - End of line</li>
<li><code>.</code> - Any character</li>
<li><code>*</code> - Zero or more</li>
<li><code>+</code> - One or more (with -E)</li>
<li><code>?</code> - Zero or one (with -E)</li>
</ul>
<h3 id="character-classes">Character Classes</h3>
<ul>
<li><code>[abc]</code> - Any of a, b, c</li>
<li><code>[^abc]</code> - Not a, b, c</li>
<li><code>[a-z]</code> - Any lowercase</li>
<li><code>[0-9]</code> - Any digit</li>
<li><code>\d</code> - Digit (with -P)</li>
<li><code>\w</code> - Word character (with -P)</li>
</ul>
<h2 id="practical-examples">Practical Examples</h2>
<h3 id="search-for-ip-addresses">Search for IP addresses:</h3>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>grep -E <span style="color:#a5d6ff">&#34;[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}&#34;</span> file.txt
</span></span></code></pre></div><h3 id="search-for-email-addresses">Search for email addresses:</h3>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>grep -E <span style="color:#a5d6ff">&#34;[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}&#34;</span> file.txt
</span></span></code></pre></div><h3 id="search-in-multiple-files">Search in multiple files:</h3>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>grep -r <span style="color:#a5d6ff">&#34;TODO&#34;</span> /path/to/project
</span></span></code></pre></div><h3 id="search-with-file-patterns">Search with file patterns:</h3>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>grep -r --include<span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;*.js&#34;</span> <span style="color:#a5d6ff">&#34;console.log&#34;</span> .
</span></span></code></pre></div><h3 id="count-occurrences">Count occurrences:</h3>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>grep -c <span style="color:#a5d6ff">&#34;error&#34;</span> /var/log/system.log
</span></span></code></pre></div><h3 id="show-only-matching-part">Show only matching part:</h3>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>grep -o <span style="color:#a5d6ff">&#34;[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}&#34;</span> access.log
</span></span></code></pre></div><h2 id="file-operations">File Operations</h2>
<h3 id="recursive-search">Recursive Search</h3>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>grep -r <span style="color:#a5d6ff">&#34;pattern&#34;</span> /path
</span></span><span style="display:flex;"><span>grep -R <span style="color:#a5d6ff">&#34;pattern&#34;</span> /path
</span></span></code></pre></div><h3 id="includeexclude-files">Include/Exclude Files</h3>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>grep -r --include<span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;*.txt&#34;</span> <span style="color:#a5d6ff">&#34;pattern&#34;</span> .
</span></span><span style="display:flex;"><span>grep -r --exclude<span style="color:#ff7b72;font-weight:bold">=</span><span style="color:#a5d6ff">&#34;*.log&#34;</span> <span style="color:#a5d6ff">&#34;pattern&#34;</span> .
</span></span></code></pre></div><h2 id="useful-combinations">Useful Combinations</h2>
<h3 id="search-for-functions-in-code">Search for functions in code:</h3>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>grep -n <span style="color:#a5d6ff">&#34;function.*(&#34;</span> *.js
</span></span></code></pre></div><h3 id="find-files-containing-pattern">Find files containing pattern:</h3>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>grep -l <span style="color:#a5d6ff">&#34;pattern&#34;</span> *.txt
</span></span></code></pre></div><h3 id="search-case-insensitive-with-line-numbers">Search case-insensitive with line numbers:</h3>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>grep -in <span style="color:#a5d6ff">&#34;error&#34;</span> /var/log/system.log
</span></span></code></pre></div><h3 id="exclude-binary-files">Exclude binary files:</h3>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>grep -I <span style="color:#a5d6ff">&#34;pattern&#34;</span> *
</span></span></code></pre></div><h3 id="search-for-multiple-patterns">Search for multiple patterns:</h3>
<div class="highlight"><pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:2;-o-tab-size:2;tab-size:2;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>grep -E <span style="color:#a5d6ff">&#34;error|warning|critical&#34;</span> /var/log/system.log
</span></span></code></pre></div><h2 id="exit-codes">Exit Codes</h2>
<ul>
<li><strong>0</strong> - Match found</li>
<li><strong>1</strong> - No match found</li>
<li><strong>2</strong> - Error occurred</li>
</ul>
<h2 id="tips-and-tricks">Tips and Tricks</h2>
<ul>
<li>Use <code>grep --help</code> for complete option list</li>
<li>Combine with other tools: <code>ps aux | grep process_name</code></li>
<li>Use aliases for common patterns: <code>alias finderr='grep -r &quot;error&quot; .'</code></li>
<li>For complex patterns, consider using <code>egrep</code> (equivalent to <code>grep -E</code>)</li>
</ul>
<h2 id="see-also">See Also</h2>
<ul>
<li><code>egrep</code> - Extended grep</li>
<li><code>fgrep</code> - Fixed string grep</li>
<li><code>rg</code> (ripgrep) - Faster alternative</li>
<li><code>ag</code> (the silver searcher) - Another fast alternative</li>
</ul>
]]></content:encoded></item></channel></rss>