Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fedora - Command line quick tips: Searching with grep

#1
Command line quick tips: Searching with grep

<div><p>If you use your Fedora system for more than just browsing the web, you have probably needed to search for text in your files. For instance, you might be a developer that can’t remember where you left some code snippet. Or you might be looking for a setting stored in your system configuration files. Whatever the reason, there are plenty of ways to search for text on your Fedora system. This article will show you how, including using the built-in utility <em>grep</em>.</p>
<p> <span id="more-22816"></span> </p>
<h2>Introducing grep</h2>
<p>The <strong>grep</strong> utility allows you to search for text, or more specifically text <em>patterns</em>, on your file system. The name <em>grep</em> comes from <em>global regular expression print</em>. Yikes, what a mouthful! This is because a <em>regular expression</em> (or <em>regex</em>) is a way of defining text patterns.</p>
<p>The <em>grep</em> utility lets you find and print out matches on these patterns — thus the name. It’s a powerful system, and you can even find it in modern code editors like <a href="https://fedoramagazine.org/using-visual-studio-code-fedora/">Visual Studio Code</a> or <a href="https://fedoramagazine.org/install-atom-fedora/">Atom</a>.</p>
<h2>Regular expressions</h2>
<p>Harnessing all the power of <a href="https://en.wikipedia.org/wiki/Regular_expression">regular expressions</a> is a topic bigger than this article, for sure. The simplest kind of regex can be just a word, or a portion of a word. That pattern is simply “the following characters, in the same order.” The pattern is searched line by line. For example:</p>
<ul>
<li><strong>pciutil</strong> – matches any time the 7 characters <em>pciutil</em> appear together — including <em>pciutil, pciutils, pciutil123,</em> and <em>foopciutil</em>.</li>
<li><strong>^pciutil</strong> – matches any time the 7 characters <em>pciutil</em> appear together immediately at the beginning of a line (that’s what the <strong>^</strong> stands for)</li>
<li><strong>pciutil$</strong> – matches any time the 7 characters <em>pciutil</em> appear together immediately before the end of a line (that’s what the <strong>$</strong> stands for)</li>
</ul>
<p>More complicated expressions are also possible. Special characters are used in a regex as wildcards, or to change the way the regex works. If you want to match on one of these characters, use a <strong>\</strong> (backslash) before the character.</p>
<p>For instance, the <strong>.</strong> (period or full stop) is a wildcard that matches any single character. If you use it in the expression <strong>pci.til</strong>, it matches <em>pciutil</em>, <em>pci4til</em>, or <em>pci!til</em>, but does not match <em>pcitil</em>. There must be a character to match the <strong>.</strong> in the regular expression.</p>
<p>The <strong>?</strong> is a marker in a regex that marks the previous element as optional. So if you built on the previous example, the expression <strong>pci.?til</strong> would also match on <em>pcitil</em> because there need not be a character between <em>i</em> and <em>t</em> for a valid match.</p>
<p>The <strong>+</strong> and <strong>*</strong> are markers that stand for repetition. While <strong>+</strong> stands for <em>one or more</em> of the previous element, <strong>*</strong> stands for <em>zero or more</em>. So the regex <strong>pci.+til</strong> would match any of these: <em>pciutil, pci4til, pci!til, pciuuuuuutil, pci423til</em>. However, it wouldn’t match <em>pcitil</em> — but the regex <strong>pci.*til</strong> would.</p>
<h2>Examples of grep</h2>
<p>Now that you know a little about regex, let’s put it to work. Imagine that you’re trying to find a configuration file that mentions a user account <em>jpublic</em>. You tried a bunch of files already, but none were the correct one, and you’re sure it’s there. So, try searching the <em>/etc</em> folder (using <em>sudo</em> because some subfolders are not readable outside the <em>root</em> account):</p>
<pre class="wp-block-preformatted">$ sudo grep -r jpublic /etc/</pre>
<p>The <em>-r</em> switch searches the folder recursively. The utility prints a list of matching files, and the line where the hit occurred. In most modern terminal environments, the hit is color highlighted for better readability.</p>
<p>Imagine you have a much larger selection of files in <em>/home/shared</em> and you need to establish which ones mention the name MacNulty. However, you’re not sure whether the capitalization will be consistent, and you’re just looking for names of files, not the context. Also, you believe someone may have misspelled the name as McNulty in some places.</p>
<p>Use the <em>-l</em> switch to only output filenames with a match, a ? marker for optional <em>a</em> in the name, and <em>-i</em> to make the search case-insensitive:</p>
<pre class="wp-block-preformatted">$ sudo grep -irl 'ma\?cnulty' /home/shared</pre>
<p>This command will match on strings like <em>Macnulty, McNulty, Mcnulty,</em> and <em>macNulty</em> with no problem. You’ll get a simple list of filenames where the match was found in the contents.</p>
<p>These are only the simplest ways to use <em>grep</em> and regular expressions. You can learn a lot more about both using the <em>info grep</em> command.</p>
<h2>But wait, there’s more…</h2>
<p>The <em>grep</em> command is venerable but in some situations may not be as efficient as newer search utilities. For instance, the <em>ripgrep</em> utility is engineered to be a fast search utility that can take the place of <em>grep</em>. We covered <em>ripgrep</em> as part of an <a href="https://fedoramagazine.org/oxidizing-fedora-try-rust-applications-today/">article on Rust and Rust applications</a> previously in the Magazine:</p>
<figure class="wp-block-embed-wordpress wp-block-embed is-type-wp-embed is-provider-fedora-magazine">
<div class="wp-block-embed__wrapper">
<blockquote class="wp-embedded-content" data-secret="0fpvhDfUAm"><p><a href="https://fedoramagazine.org/oxidizing-fedora-try-rust-applications-today/">Oxidizing Fedora: Try Rust and its applications today</a></p></blockquote>
</div>
</figure>
<p>It’s important to note that <em>ripgrep</em> has its own command line switches and syntax. For example, it has simple switches to print only filename matches, invert searches, and many other useful functions. It can also ignore based on <em>.rgignore</em> files placed in any subdirectories. (It’s also noteworthy that the <em>-r</em> switch is used differently for <em>ripgrep</em>, because it is automatically recursive.)</p>
<p>To install, use this command:</p>
<pre class="wp-block-preformatted">$ sudo dnf install ripgrep</pre>
<p>To explore the options, use the manual page (<em>man rg</em>). You’ll find that many, but not all, options are the same as <em>grep</em>.</p>
<p>Have fun searching!</p>
<hr class="wp-block-separator" />
</div>


https://www.sickgaming.net/blog/2019/08/...with-grep/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016