Skip to content

Commit d0f431f

Browse files
added 21st CLI tip
1 parent 4a67f30 commit d0f431f

File tree

10 files changed

+85
-8
lines changed

10 files changed

+85
-8
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ I'm addicted to reading fantasy/sci-fi books, so I have a [blog](https://learnby
6363

6464
## Tips
6565

66+
* [CLI tip 21: inplace file editing with GNU awk](https://learnbyexample.github.io/tips/cli-tip-21/)
6667
* [Python tip 21: sorting iterables based on a key](https://learnbyexample.github.io/tips/python-tip-21/)
6768
* [Vim tip 19: working with buffers](https://learnbyexample.github.io/tips/vim-tip-19/)
6869
* [CLI tip 20: expand and unexpand](https://learnbyexample.github.io/tips/cli-tip-20/)

Diff for: atom.xml

+45-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,52 @@
55
<link href="https://learnbyexample.github.io/atom.xml" rel="self" type="application/atom+xml"/>
66
<link href="https://learnbyexample.github.io"/>
77
<generator uri="https://www.getzola.org/">Zola</generator>
8-
<updated>2023-01-02T00:00:00+00:00</updated>
8+
<updated>2023-01-04T00:00:00+00:00</updated>
99
<id>https://learnbyexample.github.io/atom.xml</id>
10+
<entry xml:lang="en">
11+
<title>CLI tip 21: inplace file editing with GNU awk</title>
12+
<published>2023-01-04T00:00:00+00:00</published>
13+
<updated>2023-01-04T00:00:00+00:00</updated>
14+
<link rel="alternate" href="https://learnbyexample.github.io/tips/cli-tip-21/" type="text/html"/>
15+
<id>https://learnbyexample.github.io/tips/cli-tip-21/</id>
16+
<content type="html">&lt;p&gt;You can use the &lt;code&gt;-i&lt;&#x2F;code&gt; option with &lt;code&gt;GNU awk&lt;&#x2F;code&gt; to load libraries. The &lt;code&gt;inplace&lt;&#x2F;code&gt; library comes by default with the &lt;code&gt;GNU awk&lt;&#x2F;code&gt; installation. Thus, you can use &lt;code&gt;-i inplace&lt;&#x2F;code&gt; to modify the original input itself. Make sure to test that the code is working as intended before using this option.&lt;&#x2F;p&gt;
17+
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#f5f5f5;color:#1f1f1f;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;$ cat table.txt
18+
&lt;&#x2F;span&gt;&lt;span&gt;brown bread mat cake &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;42
19+
&lt;&#x2F;span&gt;&lt;span&gt;blue cake mug shirt &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;7
20+
&lt;&#x2F;span&gt;&lt;span&gt;yellow banana window shoes &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;3.14
21+
&lt;&#x2F;span&gt;&lt;span&gt;
22+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#7f8989;&quot;&gt;# retain only the first and third fields
23+
&lt;&#x2F;span&gt;&lt;span&gt;$ awk &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;i inplace &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;{print $1, $3}&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt; table.txt
24+
&lt;&#x2F;span&gt;&lt;span&gt;$ cat table.txt
25+
&lt;&#x2F;span&gt;&lt;span&gt;brown mat
26+
&lt;&#x2F;span&gt;&lt;span&gt;blue mug
27+
&lt;&#x2F;span&gt;&lt;span&gt;yellow window
28+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
29+
&lt;p&gt;You can provide a backup extension by setting the &lt;code&gt;inplace::suffix&lt;&#x2F;code&gt; special variable. For example, if the input file is &lt;code&gt;ip.txt&lt;&#x2F;code&gt; and &lt;code&gt;inplace::suffix=&#x27;.orig&#x27;&lt;&#x2F;code&gt; is used, the backup file will be named as &lt;code&gt;ip.txt.orig&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
30+
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#f5f5f5;color:#1f1f1f;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;$ cat marks.txt
31+
&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;Name Physics Maths
32+
&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;Moe &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;76 82
33+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;Raj &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;56 64
34+
&lt;&#x2F;span&gt;&lt;span&gt;
35+
&lt;&#x2F;span&gt;&lt;span&gt;$ awk &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;i inplace &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;v inplace::suffix=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;.bkp&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;v &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c23f31;&quot;&gt;OFS&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;{$1=$1} 1&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt; marks.txt
36+
&lt;&#x2F;span&gt;&lt;span&gt;$ cat marks.txt
37+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;Name&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;Physics&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;Maths
38+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;Moe&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;76&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;82
39+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;Raj&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;56&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;64
40+
&lt;&#x2F;span&gt;&lt;span&gt;
41+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#7f8989;&quot;&gt;# original file is preserved in &amp;#39;marks.txt.bkp&amp;#39;
42+
&lt;&#x2F;span&gt;&lt;span&gt;$ cat marks.txt.bkp
43+
&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;Name Physics Maths
44+
&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;Moe &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;76 82
45+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;Raj &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;56 64
46+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
47+
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;info.svg&quot; alt=&quot;info&quot; &#x2F;&gt; Earlier versions of &lt;code&gt;GNU awk&lt;&#x2F;code&gt; used &lt;code&gt;INPLACE_SUFFIX&lt;&#x2F;code&gt; variable instead of &lt;code&gt;inplace::suffix&lt;&#x2F;code&gt;. Also, you can use &lt;code&gt;inplace::enable&lt;&#x2F;code&gt; variable to dynamically control whether files should be inplaced or not. See &lt;a href=&quot;https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;gawk&#x2F;manual&#x2F;gawk.html#Extension-Sample-Inplace&quot;&gt;gawk manual: Enabling In-Place File Editing&lt;&#x2F;a&gt; for more details.&lt;&#x2F;p&gt;
48+
&lt;p&gt;&lt;strong&gt;Video demo&lt;&#x2F;strong&gt;:&lt;&#x2F;p&gt;
49+
&lt;p align=&quot;center&quot;&gt;&lt;iframe width=&quot;560&quot; height=&quot;315&quot; loading=&quot;lazy&quot; src=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;embed&#x2F;yfO-HVTBoSI&quot; title=&quot;YouTube video player&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture&quot; allowfullscreen&gt;&lt;&#x2F;iframe&gt;&lt;&#x2F;p&gt;
50+
&lt;br&gt;
51+
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;info.svg&quot; alt=&quot;info&quot; &#x2F;&gt; See my &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;learnbyexample&#x2F;learn_gnuawk&quot;&gt;GNU AWK&lt;&#x2F;a&gt; ebook if you are interested in learning about the &lt;code&gt;GNU awk&lt;&#x2F;code&gt; command in more detail.&lt;&#x2F;p&gt;
52+
</content>
53+
</entry>
1054
<entry xml:lang="en">
1155
<title>2022: year in perspective</title>
1256
<published>2022-12-30T00:00:00+00:00</published>

Diff for: sitemap.xml

+5-1
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@
406406
</url>
407407
<url>
408408
<loc>https://learnbyexample.github.io/tips/</loc>
409-
<lastmod>2022-12-28</lastmod>
409+
<lastmod>2023-01-04</lastmod>
410410
</url>
411411
<url>
412412
<loc>https://learnbyexample.github.io/tips/cli-tip-1/</loc>
@@ -460,6 +460,10 @@
460460
<loc>https://learnbyexample.github.io/tips/cli-tip-20/</loc>
461461
<lastmod>2022-12-14</lastmod>
462462
</url>
463+
<url>
464+
<loc>https://learnbyexample.github.io/tips/cli-tip-21/</loc>
465+
<lastmod>2023-01-04</lastmod>
466+
</url>
463467
<url>
464468
<loc>https://learnbyexample.github.io/tips/cli-tip-3/</loc>
465469
<lastmod>2022-06-14</lastmod>

Diff for: tags/command-line/index.html

+1-1
Large diffs are not rendered by default.

Diff for: tags/index.html

+1-1
Large diffs are not rendered by default.

Diff for: tags/linux/index.html

+1-1
Large diffs are not rendered by default.

Diff for: tags/tip/index.html

+1-1
Large diffs are not rendered by default.

Diff for: tips/cli-tip-20/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919
</span><span style=color:#7f8989;># remaining space is left as is
2020
</span><span>$ </span><span style=color:#b39f04;>printf </span><span style=color:#d07711;>' a b c\n' </span><span style=color:#72ab00;>|</span><span> unexpand </span><span style=color:#72ab00;>|</span><span> cat </span><span style=color:#72ab00;>-</span><span style=color:#5597d6;>T
2121
</span><span style=color:#72ab00;>^</span><span style=color:#5597d6;>I</span><span> a b c
22-
</span></code></pre><p><strong>Video demo</strong>:<p align=center><iframe allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" title="YouTube video player" allowfullscreen frameborder=0 height=315 loading=lazy src=https://www.youtube.com/embed/tsJCtE6oZDs width=560></iframe></p><br><p><img alt=info src=/images/info.svg> See <a href=https://learnbyexample.github.io/cli_text_processing_coreutils/expand-unexpand.html>expand and unexpand</a> chapter my <a href=https://github.com/learnbyexample/cli_text_processing_coreutils>Command line text processing with GNU Coreutils</a> ebook for more examples, options, etc.</div><div class=post-footer><div class=post-tags><a href=https://learnbyexample.github.io/tags/linux/>#linux</a><a href=https://learnbyexample.github.io/tags/command-line/>#command-line</a><a href=https://learnbyexample.github.io/tags/tip/>#tip</a></div><hr color=#e6e6e6><div class=post-nav><p><a class=next href=https://learnbyexample.github.io/tips/cli-tip-19/>CLI tip 19: extended globs →</a><br></div><hr color=#e6e6e6><p>📰 Use <a href=https://learnbyexample.github.io/atom.xml>this link</a> for the Atom feed. <br> ✅ Follow me on <a href=https://twitter.com/learn_byexample>Twitter</a>, <a href=https://github.com/learnbyexample>GitHub</a> and <a href=https://www.youtube.com/c/learnbyexample42>Youtube</a> for interesting tech nuggets. <br> 📧 Subscribe to <a href=https://learnbyexample.gumroad.com/l/learnbyexample-weekly>learnbyexample weekly</a> for programming resources, tips, tools, free ebooks and more (free newsletter, delivered every Friday).<hr color=#e6e6e6></div></article></div></main></div><script src=https://learnbyexample.github.io/even.js></script>
22+
</span></code></pre><p><strong>Video demo</strong>:<p align=center><iframe allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" title="YouTube video player" allowfullscreen frameborder=0 height=315 loading=lazy src=https://www.youtube.com/embed/tsJCtE6oZDs width=560></iframe></p><br><p><img alt=info src=/images/info.svg> See <a href=https://learnbyexample.github.io/cli_text_processing_coreutils/expand-unexpand.html>expand and unexpand</a> chapter my <a href=https://github.com/learnbyexample/cli_text_processing_coreutils>Command line text processing with GNU Coreutils</a> ebook for more examples, options, etc.</div><div class=post-footer><div class=post-tags><a href=https://learnbyexample.github.io/tags/linux/>#linux</a><a href=https://learnbyexample.github.io/tags/command-line/>#command-line</a><a href=https://learnbyexample.github.io/tags/tip/>#tip</a></div><hr color=#e6e6e6><div class=post-nav><p><a class=previous href=https://learnbyexample.github.io/tips/cli-tip-21/>← CLI tip 21: inplace file editing with GNU awk</a><br><p><a class=next href=https://learnbyexample.github.io/tips/cli-tip-19/>CLI tip 19: extended globs →</a><br></div><hr color=#e6e6e6><p>📰 Use <a href=https://learnbyexample.github.io/atom.xml>this link</a> for the Atom feed. <br> ✅ Follow me on <a href=https://twitter.com/learn_byexample>Twitter</a>, <a href=https://github.com/learnbyexample>GitHub</a> and <a href=https://www.youtube.com/c/learnbyexample42>Youtube</a> for interesting tech nuggets. <br> 📧 Subscribe to <a href=https://learnbyexample.gumroad.com/l/learnbyexample-weekly>learnbyexample weekly</a> for programming resources, tips, tools, free ebooks and more (free newsletter, delivered every Friday).<hr color=#e6e6e6></div></article></div></main></div><script src=https://learnbyexample.github.io/even.js></script>

0 commit comments

Comments
 (0)