|
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-02-28T00:00:00+00:00</updated> |
| 8 | + <updated>2023-03-07T00:00:00+00:00</updated> |
9 | 9 | <id>https://learnbyexample.github.io/atom.xml</id>
|
| 10 | + <entry xml:lang="en"> |
| 11 | + <title>CLI tip 24: inserting file contents one line at a time</title> |
| 12 | + <published>2023-03-07T00:00:00+00:00</published> |
| 13 | + <updated>2023-03-07T00:00:00+00:00</updated> |
| 14 | + <link rel="alternate" href="https://learnbyexample.github.io/tips/cli-tip-24/" type="text/html"/> |
| 15 | + <id>https://learnbyexample.github.io/tips/cli-tip-24/</id> |
| 16 | + <content type="html"><p>The <code>R</code> command provided by <code>GNU sed</code> is very similar to <code>r</code> with respect to most of the rules seen in an earlier <a href="https://learnbyexample.github.io/tips/cli-tip-18/">tip</a>. But instead of reading entire file contents, <code>R</code> will read one line at a time from the source file when the given address matches. If entire file has already been read and another address matches, <code>sed</code> will proceed as if the line was empty.</p> |
| 17 | +<p>Here's an example:</p> |
| 18 | +<pre data-lang="ruby" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-ruby "><code class="language-ruby" data-lang="ruby"><span>$ cat ip.txt |
| 19 | +</span><span> </span><span style="color:#72ab00;">*</span><span> sky |
| 20 | +</span><span> </span><span style="color:#72ab00;">*</span><span> apple |
| 21 | +</span><span>$ cat fav_colors.txt |
| 22 | +</span><span>deep red |
| 23 | +</span><span>yellow |
| 24 | +</span><span>reddish |
| 25 | +</span><span>brown |
| 26 | +</span><span> |
| 27 | +</span><span style="color:#7f8989;"># add a line from &#39;ip.txt&#39; |
| 28 | +</span><span style="color:#7f8989;"># whenever a line from &#39;fav_colors.txt&#39; contains &#39;red&#39; |
| 29 | +</span><span>$ sed </span><span style="color:#d07711;">&#39;/red/R ip.txt&#39;</span><span> fav_colors.txt |
| 30 | +</span><span>deep red |
| 31 | +</span><span> </span><span style="color:#72ab00;">*</span><span> sky |
| 32 | +</span><span>yellow |
| 33 | +</span><span>reddish |
| 34 | +</span><span> </span><span style="color:#72ab00;">*</span><span> apple |
| 35 | +</span><span>brown |
| 36 | +</span></code></pre> |
| 37 | +<p>You can combine with other <code>sed</code> commands to solve various kind of problems. For example, to replace the matching lines:</p> |
| 38 | +<pre data-lang="ruby" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-ruby "><code class="language-ruby" data-lang="ruby"><span style="color:#7f8989;"># empty // will refer to the previously used regex, /red/ in this case |
| 39 | +</span><span>$ sed </span><span style="color:#72ab00;">-</span><span>e </span><span style="color:#d07711;">&#39;/red/R ip.txt&#39; </span><span style="color:#72ab00;">-</span><span>e </span><span style="color:#d07711;">&#39;//d&#39;</span><span> fav_colors.txt |
| 40 | +</span><span> </span><span style="color:#72ab00;">*</span><span> sky |
| 41 | +</span><span>yellow |
| 42 | +</span><span> </span><span style="color:#72ab00;">*</span><span> apple |
| 43 | +</span><span>brown |
| 44 | +</span></code></pre> |
| 45 | +<p>And, here's how you can interleave contents of two files:</p> |
| 46 | +<pre data-lang="ruby" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-ruby "><code class="language-ruby" data-lang="ruby"><span style="color:#7f8989;"># /dev/stdin will get data from stdin (output of &#39;seq 4&#39; here) |
| 47 | +</span><span style="color:#7f8989;"># same as: seq 4 | paste -d&#39;\n&#39; fav_colors.txt - |
| 48 | +</span><span>$ seq </span><span style="color:#b3933a;">4 </span><span style="color:#72ab00;">|</span><span> sed </span><span style="color:#d07711;">&#39;R /dev/stdin&#39;</span><span> fav_colors.txt |
| 49 | +</span><span>deep red |
| 50 | +</span><span style="color:#b3933a;">1 |
| 51 | +</span><span>yellow |
| 52 | +</span><span style="color:#b3933a;">2 |
| 53 | +</span><span>reddish |
| 54 | +</span><span style="color:#b3933a;">3 |
| 55 | +</span><span>brown |
| 56 | +</span><span style="color:#b3933a;">4 |
| 57 | +</span><span> |
| 58 | +</span><span style="color:#7f8989;"># using &#39;paste&#39; here will add a newline when stdin runs out of data |
| 59 | +</span><span>$ seq </span><span style="color:#b3933a;">2 </span><span style="color:#72ab00;">|</span><span> sed </span><span style="color:#d07711;">&#39;R /dev/stdin&#39;</span><span> fav_colors.txt |
| 60 | +</span><span>deep red |
| 61 | +</span><span style="color:#b3933a;">1 |
| 62 | +</span><span>yellow |
| 63 | +</span><span style="color:#b3933a;">2 |
| 64 | +</span><span>reddish |
| 65 | +</span><span>brown |
| 66 | +</span></code></pre> |
| 67 | +<p><strong>Video demo</strong>:</p> |
| 68 | +<p align="center"><iframe width="560" height="315" loading="lazy" src="https://www.youtube.com/embed/W7LvN7X6Rfg" title="YouTube video player" frameborder="0" allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p> |
| 69 | +<br> |
| 70 | +<p><img src="/images/info.svg" alt="info" /> See also my <a href="https://github.com/learnbyexample/learn_gnused">GNU sed</a> ebook.</p> |
| 71 | +</content> |
| 72 | + </entry> |
10 | 73 | <entry xml:lang="en">
|
11 | 74 | <title>Python tip 24: modifying list using insert and slice</title>
|
12 | 75 | <published>2023-02-28T00:00:00+00:00</published>
|
|
0 commit comments