<?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>Search · QuickRef</title><link>https://qref.eknath.dev/tags/search/</link><description>Search updates from QuickRef</description><language>en-us</language><atom:link href="https://qref.eknath.dev/tags/search/index.xml" rel="self" type="application/rss+xml"/><lastBuildDate>Sun, 01 Jan 2023 00:00:00 +0000</lastBuildDate><item><title>Regex</title><link>https://qref.eknath.dev/tools/regex/</link><guid isPermaLink="true">https://qref.eknath.dev/tools/regex/</guid><pubDate>Sun, 01 Jan 2023 00:00:00 +0000</pubDate><category>regex</category><category>pattern-matching</category><category>text-processing</category><category>programming</category><category>search</category><description>Regular expressions for pattern matching and text processing in various programming languages and tools.</description><content:encoded><![CDATA[<h2 id="overview">Overview</h2>
<p>Regular expressions (regex) are patterns used to match character combinations in strings. They&rsquo;re essential for text processing, validation, searching, and data extraction across virtually all programming languages and tools.</p>
<h2 id="basic-syntax">Basic Syntax</h2>
<h3 id="literal-characters">Literal Characters</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex">hello       # Matches &#34;hello&#34; exactly
123         # Matches &#34;123&#34; exactly
</code></pre><h3 id="meta-characters">Meta Characters</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex">.           # Any character except newline
^           # Start of string/line
$           # End of string/line
\           # Escape character
|           # OR operator
()          # Group
[]          # Character class
{}          # Quantifier
</code></pre><h2 id="character-classes">Character Classes</h2>
<h3 id="predefined-classes">Predefined Classes</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex">.           # Any character except newline
\d          # Any digit (0-9)
\D          # Any non-digit
\w          # Any word character (a-z, A-Z, 0-9, _)
\W          # Any non-word character
\s          # Any whitespace character
\S          # Any non-whitespace character
</code></pre><h3 id="custom-classes">Custom Classes</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex">[abc]       # Any of a, b, or c
[a-z]       # Any lowercase letter
[A-Z]       # Any uppercase letter
[0-9]       # Any digit
[^abc]      # Any character except a, b, or c
[a-zA-Z0-9] # Any alphanumeric character
</code></pre><h2 id="quantifiers">Quantifiers</h2>
<h3 id="basic-quantifiers">Basic Quantifiers</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex">*           # 0 or more
+           # 1 or more
?           # 0 or 1 (optional)
{n}         # Exactly n times
{n,}        # n or more times
{n,m}       # Between n and m times
</code></pre><h3 id="examples">Examples</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex">a*          # &#34;&#34;, &#34;a&#34;, &#34;aa&#34;, &#34;aaa&#34;, ...
a+          # &#34;a&#34;, &#34;aa&#34;, &#34;aaa&#34;, ... (not empty)
a?          # &#34;&#34; or &#34;a&#34;
a{3}        # &#34;aaa&#34; exactly
a{2,4}      # &#34;aa&#34;, &#34;aaa&#34;, or &#34;aaaa&#34;
</code></pre><h2 id="anchors">Anchors</h2>
<h3 id="position-anchors">Position Anchors</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex">^           # Start of string/line
$           # End of string/line
\b          # Word boundary
\B          # Non-word boundary
</code></pre><h3 id="examples-1">Examples</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex">^hello      # &#34;hello&#34; at start of line
world$      # &#34;world&#34; at end of line
\bcat\b     # &#34;cat&#34; as whole word
\Bcat\B     # &#34;cat&#34; not as whole word
</code></pre><h2 id="groups-and-capturing">Groups and Capturing</h2>
<h3 id="groups">Groups</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex">(abc)       # Capture group
(?:abc)     # Non-capturing group
(?P&lt;name&gt;abc) # Named group (Python)
(?&lt;name&gt;abc)  # Named group (C#, Java)
</code></pre><h3 id="backreferences">Backreferences</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex">(cat)\1     # Matches &#34;catcat&#34;
(\w+)\s+\1  # Matches repeated words
</code></pre><h2 id="common-patterns">Common Patterns</h2>
<h3 id="email-validation">Email Validation</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex">^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
</code></pre><h3 id="phone-numbers">Phone Numbers</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex">^\+?1?[-.\s]?\(?[0-9]{3}\)?[-.\s]?[0-9]{3}[-.\s]?[0-9]{4}$
</code></pre><h3 id="urls">URLs</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex">^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&amp;//=]*)$
</code></pre><h3 id="ipv4-address">IPv4 Address</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex">^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
</code></pre><h3 id="date-formats">Date Formats</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex"># MM/DD/YYYY
^(0[1-9]|1[0-2])\/(0[1-9]|[12][0-9]|3[01])\/\d{4}$

# YYYY-MM-DD
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$
</code></pre><h3 id="credit-card-numbers">Credit Card Numbers</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex"># Visa
^4[0-9]{12}(?:[0-9]{3})?$

# MasterCard
^5[1-5][0-9]{14}$

# American Express
^3[47][0-9]{13}$
</code></pre><h2 id="language-specific-usage">Language-Specific Usage</h2>
<h3 id="javascript">JavaScript</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-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic">// Create regex
</span></span></span><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"></span><span style="color:#ff7b72">const</span> regex <span style="color:#ff7b72;font-weight:bold">=</span> <span style="color:#f85149">/pattern/flags;</span>
</span></span><span style="display:flex;"><span><span style="color:#ff7b72">const</span> regex <span style="color:#ff7b72;font-weight:bold">=</span> <span style="color:#ff7b72">new</span> RegExp(<span style="color:#a5d6ff">&#39;pattern&#39;</span>, <span style="color:#a5d6ff">&#39;flags&#39;</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic">// Test match
</span></span></span><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"></span>regex.test(string);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic">// Find matches
</span></span></span><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"></span>string.match(regex);
</span></span><span style="display:flex;"><span>string.search(regex);
</span></span><span style="display:flex;"><span>string.replace(regex, replacement);
</span></span></code></pre></div><h3 id="python">Python</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-python" data-lang="python"><span style="display:flex;"><span><span style="color:#ff7b72">import</span> <span style="color:#ff7b72">re</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Compile regex</span>
</span></span><span style="display:flex;"><span>pattern <span style="color:#ff7b72;font-weight:bold">=</span> re<span style="color:#ff7b72;font-weight:bold">.</span>compile(<span style="color:#79c0ff">r</span><span style="color:#a5d6ff">&#39;regex_pattern&#39;</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Match functions</span>
</span></span><span style="display:flex;"><span>re<span style="color:#ff7b72;font-weight:bold">.</span><span style="color:#ff7b72">match</span>(pattern, string)    <span style="color:#8b949e;font-style:italic"># Match at beginning</span>
</span></span><span style="display:flex;"><span>re<span style="color:#ff7b72;font-weight:bold">.</span>search(pattern, string)   <span style="color:#8b949e;font-style:italic"># Find first match</span>
</span></span><span style="display:flex;"><span>re<span style="color:#ff7b72;font-weight:bold">.</span>findall(pattern, string)  <span style="color:#8b949e;font-style:italic"># Find all matches</span>
</span></span><span style="display:flex;"><span>re<span style="color:#ff7b72;font-weight:bold">.</span>sub(pattern, replacement, string)  <span style="color:#8b949e;font-style:italic"># Replace</span>
</span></span></code></pre></div><h3 id="bashgrep">Bash/grep</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><span style="color:#8b949e;font-style:italic"># Basic grep</span>
</span></span><span style="display:flex;"><span>grep <span style="color:#a5d6ff">&#39;pattern&#39;</span> file.txt
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Extended regex</span>
</span></span><span style="display:flex;"><span>grep -E <span style="color:#a5d6ff">&#39;pattern&#39;</span> file.txt
</span></span><span style="display:flex;"><span>egrep <span style="color:#a5d6ff">&#39;pattern&#39;</span> file.txt
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Perl-compatible regex</span>
</span></span><span style="display:flex;"><span>grep -P <span style="color:#a5d6ff">&#39;pattern&#39;</span> file.txt
</span></span></code></pre></div><h3 id="sed">sed</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><span style="color:#8b949e;font-style:italic"># Replace with regex</span>
</span></span><span style="display:flex;"><span>sed <span style="color:#a5d6ff">&#39;s/pattern/replacement/g&#39;</span> file.txt
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Extended regex</span>
</span></span><span style="display:flex;"><span>sed -E <span style="color:#a5d6ff">&#39;s/pattern/replacement/g&#39;</span> file.txt
</span></span></code></pre></div><h2 id="flagsmodifiers">Flags/Modifiers</h2>
<h3 id="common-flags">Common Flags</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex">i           # Case insensitive
g           # Global (find all matches)
m           # Multiline (^ and $ match line breaks)
s           # Dot matches newline
x           # Extended (ignore whitespace)
</code></pre><h3 id="examples-2">Examples</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-javascript" data-lang="javascript"><span style="display:flex;"><span><span style="color:#79c0ff">/hello/i</span>        <span style="color:#f85149">#</span> Case insensitive
</span></span><span style="display:flex;"><span><span style="color:#79c0ff">/hello/g</span>        <span style="color:#f85149">#</span> Global search
</span></span><span style="display:flex;"><span><span style="color:#79c0ff">/hello/gi</span>       <span style="color:#f85149">#</span> Case insensitive <span style="color:#ff7b72;font-weight:bold">+</span> global
</span></span></code></pre></div><h2 id="advanced-features">Advanced Features</h2>
<h3 id="lookaheadlookbehind">Lookahead/Lookbehind</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex">(?=pattern)     # Positive lookahead
(?!pattern)     # Negative lookahead
(?&lt;=pattern)    # Positive lookbehind
(?&lt;!pattern)    # Negative lookbehind
</code></pre><h3 id="examples-3">Examples</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex">\d+(?=px)       # Numbers followed by &#34;px&#34;
\d+(?!px)       # Numbers not followed by &#34;px&#34;
(?&lt;=\$)\d+      # Numbers preceded by &#34;$&#34;
(?&lt;!\$)\d+      # Numbers not preceded by &#34;$&#34;
</code></pre><h2 id="practical-examples">Practical Examples</h2>
<h3 id="extract-domain-from-email">Extract Domain from Email</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex">@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})
</code></pre><h3 id="find-html-tags">Find HTML Tags</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex">&lt;\/?[a-zA-Z][^&gt;]*&gt;
</code></pre><h3 id="match-quoted-strings">Match Quoted Strings</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex">&#34;([^&#34;\\]|\\.)*&#34;
</code></pre><h3 id="find-css-colors">Find CSS Colors</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex">#[0-9a-fA-F]{3,6}
</code></pre><h3 id="extract-urls-from-text">Extract URLs from Text</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex">https?:\/\/[^\s]+
</code></pre><h3 id="match-json-values">Match JSON Values</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex">&#34;[^&#34;]*&#34;:\s*(&#34;[^&#34;]*&#34;|\d+|true|false|null)
</code></pre><h2 id="testing-and-debugging">Testing and Debugging</h2>
<h3 id="online-tools">Online Tools</h3>
<ul>
<li><a href="https://regex101.com">regex101.com</a> - Interactive regex tester</li>
<li><a href="https://regexr.com">regexr.com</a> - Visual regex builder</li>
<li><a href="https://regexpal.com">regexpal.com</a> - Simple regex tester</li>
</ul>
<h3 id="command-line-testing">Command Line Testing</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><span style="color:#8b949e;font-style:italic"># Test with grep</span>
</span></span><span style="display:flex;"><span>echo <span style="color:#a5d6ff">&#34;test string&#34;</span> | grep -E <span style="color:#a5d6ff">&#39;pattern&#39;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Test with sed</span>
</span></span><span style="display:flex;"><span>echo <span style="color:#a5d6ff">&#34;test string&#34;</span> | sed <span style="color:#a5d6ff">&#39;s/pattern/replacement/&#39;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#8b949e;font-style:italic"># Test with Python</span>
</span></span><span style="display:flex;"><span>python3 -c <span style="color:#a5d6ff">&#34;import re; print(re.search(r&#39;pattern&#39;, &#39;test string&#39;))&#34;</span>
</span></span></code></pre></div><h2 id="performance-tips">Performance Tips</h2>
<h3 id="best-practices">Best Practices</h3>
<ul>
<li>Use specific characters instead of <code>.</code> when possible</li>
<li>Avoid nested quantifiers like <code>(a+)+</code></li>
<li>Use non-capturing groups <code>(?:...)</code> when you don&rsquo;t need the match</li>
<li>Anchor patterns with <code>^</code> and <code>$</code> when appropriate</li>
<li>Use word boundaries <code>\b</code> for word matching</li>
</ul>
<h3 id="common-pitfalls">Common Pitfalls</h3>
<ul>
<li><strong>Greedy vs Non-greedy</strong>: <code>.*</code> vs <code>.*?</code></li>
<li><strong>Backtracking</strong>: Avoid complex nested patterns</li>
<li><strong>Case sensitivity</strong>: Remember to use <code>i</code> flag when needed</li>
<li><strong>Escaping</strong>: Don&rsquo;t forget to escape special characters</li>
</ul>
<h2 id="quick-reference">Quick Reference</h2>
<h3 id="most-common-patterns">Most Common Patterns</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex">\d+             # One or more digits
\w+             # One or more word characters
\s+             # One or more whitespace
[a-zA-Z]+       # One or more letters
\b\w+\b         # Whole words
^.+$            # Entire line
.*              # Any characters (greedy)
.*?             # Any characters (non-greedy)
</code></pre><h3 id="escape-sequences">Escape Sequences</h3>
<pre tabindex="0"><code class="language-regex" data-lang="regex">\.              # Literal dot
\*              # Literal asterisk
\+              # Literal plus
\?              # Literal question mark
\\              # Literal backslash
\(              # Literal parenthesis
\[              # Literal bracket
\{              # Literal brace
\|              # Literal pipe
</code></pre><h2 id="see-also">See Also</h2>
<ul>
<li><code>man grep</code> - Pattern matching in files</li>
<li><code>man sed</code> - Stream editor with regex support</li>
<li><code>man awk</code> - Pattern scanning and processing</li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions">MDN Regex Guide</a></li>
<li><a href="https://docs.python.org/3/library/re.html">Python re module</a></li>
</ul>
]]></content:encoded></item><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>