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