|
5 | 5 | <link href="https://learnbyexample.github.io/atom.xml" rel="self" type="application/atom+xml"/>
|
6 | 6 | <link href="https://learnbyexample.github.io"/>
|
7 | 7 | <generator uri="https://www.getzola.org/">Zola</generator>
|
8 |
| - <updated>2023-01-21T00:00:00+00:00</updated> |
| 8 | + <updated>2023-01-25T00:00:00+00:00</updated> |
9 | 9 | <id>https://learnbyexample.github.io/atom.xml</id>
|
| 10 | + <entry xml:lang="en"> |
| 11 | + <title>CLI tip 22: grep options to suppress stdout and stderr</title> |
| 12 | + <published>2023-01-25T00:00:00+00:00</published> |
| 13 | + <updated>2023-01-25T00:00:00+00:00</updated> |
| 14 | + <link rel="alternate" href="https://learnbyexample.github.io/tips/cli-tip-22/" type="text/html"/> |
| 15 | + <id>https://learnbyexample.github.io/tips/cli-tip-22/</id> |
| 16 | + <content type="html"><p>While writing scripts, sometimes you just need to know if a file contains the pattern and act based on the exit status of the command. Instead of redirecting the output to <code>/dev/null</code> you can use the <code>-q</code> option. This will avoid printing anything on <code>stdout</code> and also provides speed benefit as processing would be stopped as soon as the given condition is satisfied.</p> |
| 17 | +<pre data-lang="ruby" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-ruby "><code class="language-ruby" data-lang="ruby"><span>$ cat find.txt |
| 18 | +</span><span style="color:#5597d6;">The</span><span> find command is more versatile than recursive options </span><span style="color:#72ab00;">and |
| 19 | +</span><span style="color:#72ab00;">and </span><span style="color:#b39f04;">extended</span><span> globs. </span><span style="color:#5597d6;">Apart</span><span> from searching based on filename, it |
| 20 | +</span><span>has provisions to </span><span style="color:#b39f04;">match</span><span> based on the the file characteristics |
| 21 | +</span><span>like size </span><span style="color:#72ab00;">and</span><span> time. |
| 22 | +</span><span> |
| 23 | +</span><span>$ grep </span><span style="color:#72ab00;">-</span><span>wE </span><span style="color:#d07711;">&#39;(\w+) \1&#39;</span><span> find.txt |
| 24 | +</span><span>has provisions to </span><span style="color:#b39f04;">match</span><span> based on the the file characteristics |
| 25 | +</span><span>$ grep </span><span style="color:#72ab00;">-</span><span>qwE </span><span style="color:#d07711;">&#39;(\w+) \1&#39;</span><span> find.txt |
| 26 | +</span><span>$ echo </span><span style="color:#5597d6;">$? |
| 27 | +</span><span style="color:#b3933a;">0 |
| 28 | +</span><span> |
| 29 | +</span><span>$ grep </span><span style="color:#72ab00;">-</span><span>q </span><span style="color:#d07711;">&#39;xyz&#39;</span><span> find.txt |
| 30 | +</span><span>$ echo </span><span style="color:#5597d6;">$? |
| 31 | +</span><span style="color:#b3933a;">1 |
| 32 | +</span><span> |
| 33 | +</span><span>$ grep </span><span style="color:#72ab00;">-</span><span>qwE </span><span style="color:#d07711;">&#39;(\w+) \1&#39;</span><span> find.txt </span><span style="color:#72ab00;">&amp;&amp;</span><span> echo </span><span style="color:#d07711;">&#39;Repeated words found!&#39; |
| 34 | +</span><span style="color:#5597d6;">Repeated</span><span> words found! |
| 35 | +</span></code></pre> |
| 36 | +<p>The <code>-s</code> option will suppress the error messages that are intended for the <code>stderr</code> stream.</p> |
| 37 | +<pre data-lang="ruby" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-ruby "><code class="language-ruby" data-lang="ruby"><span style="color:#7f8989;"># when the input file doesn&#39;t exist |
| 38 | +</span><span>$ grep </span><span style="color:#d07711;">&#39;in&#39;</span><span> xyz.txt |
| 39 | +</span><span style="color:#b3933a;">grep:</span><span> xyz.txt</span><span style="color:#72ab00;">: </span><span style="color:#5597d6;">No</span><span> such file </span><span style="color:#72ab00;">or</span><span> directory |
| 40 | +</span><span>$ grep </span><span style="color:#72ab00;">-</span><span>s </span><span style="color:#d07711;">&#39;in&#39;</span><span> xyz.txt |
| 41 | +</span><span>$ echo </span><span style="color:#5597d6;">$? |
| 42 | +</span><span style="color:#b3933a;">2 |
| 43 | +</span><span> |
| 44 | +</span><span style="color:#7f8989;"># when sufficient permission is not available |
| 45 | +</span><span>$ touch </span><span style="color:#72ab00;">new</span><span>.txt |
| 46 | +</span><span>$ chmod </span><span style="color:#72ab00;">-</span><span>r </span><span style="color:#72ab00;">new</span><span>.txt |
| 47 | +</span><span>$ grep </span><span style="color:#d07711;">&#39;rose&#39; </span><span style="color:#72ab00;">new</span><span>.txt |
| 48 | +</span><span style="color:#b3933a;">grep: </span><span style="color:#72ab00;">new</span><span>.txt</span><span style="color:#72ab00;">: </span><span style="color:#5597d6;">Permission</span><span> denied |
| 49 | +</span><span>$ grep </span><span style="color:#72ab00;">-</span><span>s </span><span style="color:#d07711;">&#39;rose&#39; </span><span style="color:#72ab00;">new</span><span>.txt |
| 50 | +</span><span>$ echo </span><span style="color:#5597d6;">$? |
| 51 | +</span><span style="color:#b3933a;">2 |
| 52 | +</span></code></pre> |
| 53 | +<p>Errors regarding regular expressions and invalid options will be on the <code>stderr</code> stream even when the <code>-s</code> option is used.</p> |
| 54 | +<pre data-lang="ruby" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-ruby "><code class="language-ruby" data-lang="ruby"><span>$ grep </span><span style="color:#72ab00;">-</span><span>sE </span><span style="color:#d07711;">&#39;a(&#39;</span><span> find.txt |
| 55 | +</span><span style="color:#b3933a;">grep: </span><span style="color:#5597d6;">Unmatched </span><span>( </span><span style="color:#72ab00;">or</span><span> \( |
| 56 | +</span><span> |
| 57 | +</span><span>$ grep </span><span style="color:#72ab00;">-</span><span>sE </span><span style="color:#d07711;">&#39;a(&#39;</span><span> find.txt </span><span style="color:#b3933a;">2</span><span style="color:#72ab00;">&gt; </span><span style="color:#c49a39;">/dev/</span><span style="color:#72ab00;">nu</span><span>ll |
| 58 | +</span><span>$ echo </span><span style="color:#5597d6;">$? |
| 59 | +</span><span style="color:#b3933a;">2 |
| 60 | +</span></code></pre> |
| 61 | +<p><img src="/images/info.svg" alt="info" /> Check out my <a href="https://github.com/learnbyexample/command_help/blob/master/ch"><code>ch</code> command line tool</a> for a practical example of using the <code>-q</code> option.</p> |
| 62 | +<p><strong>Video demo</strong>:</p> |
| 63 | +<p align="center"><iframe width="560" height="315" loading="lazy" src="https://www.youtube.com/embed/Pjud7hEjZ6Q" title="YouTube video player" frameborder="0" allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p> |
| 64 | +<br> |
| 65 | +<p><img src="/images/info.svg" alt="info" /> See my <a href="https://github.com/learnbyexample/learn_gnugrep_ripgrep">GNU GREP and RIPGREP</a> ebook if you are interested in learning about <code>GNU grep</code> and <code>ripgrep</code> commands in more detail.</p> |
| 66 | +</content> |
| 67 | + </entry> |
10 | 68 | <entry xml:lang="en">
|
11 | 69 | <title>Python Regular Expressions Gotchas</title>
|
12 | 70 | <published>2023-01-21T00:00:00+00:00</published>
|
13 | 71 | <updated>2023-01-21T00:00:00+00:00</updated>
|
14 | 72 | <link rel="alternate" href="https://learnbyexample.github.io/python-regex-surprises/" type="text/html"/>
|
15 | 73 | <id>https://learnbyexample.github.io/python-regex-surprises/</id>
|
16 |
| - <content type="html"><p>Regular expressions can get quite complicated and cryptic. So, it is natural to assume you have made a mistake if something isn't working as expected. However, sometimes it might just be one of the quirky corner cases discussed in this post.</p> |
| 74 | + <content type="html"><p>Regular expressions can get quite complicated and cryptic. And sometimes it might become worse due to one of the quirky corner cases discussed in this post.</p> |
17 | 75 | <p align="center"><img src="/images/python_regex_gotchas.png" alt="Python Regular Expressions Gotchas" /></p>
|
18 | 76 | <p align="center"><i>Poster created using <a href="https://www.canva.com/">Canva</a></i></p>
|
19 | 77 | <p>If you are not familiar with regular expressions, check out my <a href="https://github.com/learnbyexample/py_regular_expressions">Understanding Python re(gex)?</a> ebook.</p>
|
|
0 commit comments