<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:og="http://ogp.me/ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:schema="http://schema.org/" xmlns:sioc="http://rdfs.org/sioc/ns#" xmlns:sioct="http://rdfs.org/sioc/types#" xmlns:skos="http://www.w3.org/2004/02/skos/core#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#" version="2.0" xml:base="https://www.linuxjournal.com/tag/regular-expressions">
  <channel>
    <title>regular expressions</title>
    <link>https://www.linuxjournal.com/tag/regular-expressions</link>
    <description/>
    <language>en</language>
    
    <item>
  <title>Globbing and Regex: So Similar, So Different</title>
  <link>https://www.linuxjournal.com/content/globbing-and-regex-so-similar-so-different</link>
  <description>  &lt;div data-history-node-id="1340067" class="layout layout--onecol"&gt;
    &lt;div class="layout__region layout__region--content"&gt;
      
            &lt;div class="field field--name-field-node-image field--type-image field--label-hidden field--item"&gt;  &lt;img src="https://www.linuxjournal.com/sites/default/files/nodeimage/story/bigstock--187641571.jpg" width="600" height="600" alt="""" typeof="foaf:Image" class="img-responsive" /&gt;&lt;/div&gt;
      
            &lt;div class="field field--name-node-author field--type-ds field--label-hidden field--item"&gt;by &lt;a title="View user profile." href="https://www.linuxjournal.com/users/shawn-powers" lang="" about="https://www.linuxjournal.com/users/shawn-powers" typeof="schema:Person" property="schema:name" datatype="" xml:lang=""&gt;Shawn Powers&lt;/a&gt;&lt;/div&gt;
      
            &lt;div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"&gt;&lt;p&gt;&lt;em&gt;Grepping is awesome, as long as you don't glob it up! This article covers some grep
and regex basics.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;
There are generally two types of coffee drinkers. The first type buys a can
of pre-ground beans and uses the included scoop to make their automatic
drip coffee in the morning. The second type picks single-origin beans from
various parts of the world, accepts only beans that have been roasted
within the past week and grinds those beans with a conical burr grinder
moments before brewing in any number of complicated methods. Text searching
is a bit like that.
&lt;/p&gt;

&lt;p&gt;
For most things on the command line, people think of *.* or
*.txt and are happy to use file globbing to select the files they want. When
it comes to grepping a log file, however, you need to get a little fancier.
The confusing part is when the syntax of globbing and regex overlap.
Thankfully, it's not hard to figure out when to use which construct.
&lt;/p&gt;

&lt;h3&gt;
Globbing&lt;/h3&gt;

&lt;p&gt;
The command shell uses globbing for filename completion. If you type
something like &lt;code&gt;ls *.txt&lt;/code&gt;, you'll get a list of all the files that end in
.txt in the current directory. If you do &lt;code&gt;ls R*.txt&lt;/code&gt;, you'll get all the
files that start with capital R and have the .txt extension. The asterisk
is a wild card that lets you quickly filter which files you mean.
&lt;/p&gt;

&lt;p&gt;
You also can use a question mark in globbing if you want to specify a
single character. So, typing &lt;code&gt;ls read??.txt&lt;/code&gt; will list readme.txt, but not
read.txt. That's different from &lt;code&gt;ls read*.txt&lt;/code&gt;, which will match both
readme.txt and read.txt, because the asterisk means "zero or more
characters" in the file glob.
&lt;/p&gt;

&lt;p&gt;
Here's the easy way to remember if you're using globbing (which is very
simple) vs. regular expressions: globbing is done to filenames by the
shell, and regex is used for searching text. The only frustrating exception
to this is that sometimes the shell is too smart and conveniently does
globbing when you don't want it to—for example:

&lt;/p&gt;&lt;pre&gt;
&lt;code&gt;
grep file* README.TXT
&lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;
In most cases, this will search the file README.TXT looking for the regular
expression &lt;code&gt;file*&lt;/code&gt;, which is what you normally want. But if there happens to
be a file in the current folder that matches the &lt;code&gt;file*&lt;/code&gt; glob (let's say
filename.txt), the shell will assume you meant to pass that to
&lt;code&gt;grep&lt;/code&gt;, and so
grep actually will see:

&lt;/p&gt;&lt;pre&gt;
&lt;code&gt;
grep filename.txt README.TXT
&lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;
Gee, thank you so much Mr. Shell, but that's not what I wanted to do. For
that reason, I recommend &lt;em&gt;always&lt;/em&gt; using quotation marks when using
&lt;code&gt;grep&lt;/code&gt;. 99%
of the time you won't get an accidental glob match, but that 1% can be
infuriating. So when using &lt;code&gt;grep&lt;/code&gt;, this is much safer:

&lt;/p&gt;&lt;pre&gt;
&lt;code&gt;
grep "file*" README.TXT
&lt;/code&gt;
&lt;/pre&gt;


&lt;p&gt;
Because even if there is a filename.txt, the shell won't
substitute it automatically.
&lt;/p&gt;&lt;/div&gt;
      
            &lt;div class="field field--name-node-link field--type-ds field--label-hidden field--item"&gt;  &lt;a href="https://www.linuxjournal.com/content/globbing-and-regex-so-similar-so-different" hreflang="en"&gt;Go to Full Article&lt;/a&gt;
&lt;/div&gt;
      
    &lt;/div&gt;
  &lt;/div&gt;

</description>
  <pubDate>Mon, 17 Sep 2018 13:51:02 +0000</pubDate>
    <dc:creator>Shawn Powers</dc:creator>
    <guid isPermaLink="false">1340067 at https://www.linuxjournal.com</guid>
    </item>

  </channel>
</rss>
