|
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>2022-11-01T00:00:00+00:00</updated> |
| 8 | + <updated>2022-11-02T00:00:00+00:00</updated> |
9 | 9 | <id>https://learnbyexample.github.io/atom.xml</id>
|
| 10 | + <entry xml:lang="en"> |
| 11 | + <title>CLI tip 18: inserting file contents using GNU sed</title> |
| 12 | + <published>2022-11-02T00:00:00+00:00</published> |
| 13 | + <updated>2022-11-02T00:00:00+00:00</updated> |
| 14 | + <link rel="alternate" href="https://learnbyexample.github.io/tips/cli-tip-18/" type="text/html"/> |
| 15 | + <id>https://learnbyexample.github.io/tips/cli-tip-18/</id> |
| 16 | + <content type="html"><p>The <code>r</code> command accepts a filename as argument and when the address is satisfied, entire contents of the given file is added <em>after</em> the matching line. This is a robust way to add multiline text literally.</p> |
| 17 | +<pre data-lang="ruby" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-ruby "><code class="language-ruby" data-lang="ruby"><span>$ cat ip.txt |
| 18 | +</span><span> </span><span style="color:#72ab00;">*</span><span> sky |
| 19 | +</span><span> </span><span style="color:#72ab00;">*</span><span> apple |
| 20 | +</span><span>$ cat fav_colors.txt |
| 21 | +</span><span>deep red |
| 22 | +</span><span>yellow |
| 23 | +</span><span>reddish |
| 24 | +</span><span>brown |
| 25 | +</span><span> |
| 26 | +</span><span style="color:#7f8989;"># space between r and filename is optional |
| 27 | +</span><span style="color:#7f8989;"># adds entire contents of &#39;ip.txt&#39; after each line containing &#39;red&#39; |
| 28 | +</span><span>$ sed </span><span style="color:#d07711;">&#39;/red/r ip.txt&#39;</span><span> fav_colors.txt |
| 29 | +</span><span>deep red |
| 30 | +</span><span> </span><span style="color:#72ab00;">*</span><span> sky |
| 31 | +</span><span> </span><span style="color:#72ab00;">*</span><span> apple |
| 32 | +</span><span>yellow |
| 33 | +</span><span>reddish |
| 34 | +</span><span> </span><span style="color:#72ab00;">*</span><span> sky |
| 35 | +</span><span> </span><span style="color:#72ab00;">*</span><span> apple |
| 36 | +</span><span>brown |
| 37 | +</span></code></pre> |
| 38 | +<p>The <code>e</code> flag is the easiest way to insert file contents <em>before</em> the matching lines. Similar to the <code>r</code> command, the output of an external command (<code>cat</code> in the below example) is inserted literally.</p> |
| 39 | +<pre data-lang="ruby" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-ruby "><code class="language-ruby" data-lang="ruby"><span>$ sed </span><span style="color:#d07711;">&#39;/red/e cat ip.txt&#39;</span><span> fav_colors.txt |
| 40 | +</span><span> </span><span style="color:#72ab00;">*</span><span> sky |
| 41 | +</span><span> </span><span style="color:#72ab00;">*</span><span> apple |
| 42 | +</span><span>deep red |
| 43 | +</span><span>yellow |
| 44 | +</span><span> </span><span style="color:#72ab00;">*</span><span> sky |
| 45 | +</span><span> </span><span style="color:#72ab00;">*</span><span> apple |
| 46 | +</span><span>reddish |
| 47 | +</span><span>brown |
| 48 | +</span></code></pre> |
| 49 | +<p><img src="/images/info.svg" alt="info" /> See <a href="https://learnbyexample.github.io/learn_gnused/adding-content-from-file.html">Adding content from file</a> chapter from my <strong>GNU sed</strong> ebook for many more examples, gotchas, details about the <code>R</code> command and so on.</p> |
| 50 | +<p><strong>Video demo</strong>:</p> |
| 51 | +<p align="center"><iframe width="560" height="315" loading="lazy" src="https://www.youtube.com/embed/p1CFOCs3gGM" title="YouTube video player" frameborder="0" allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p> |
| 52 | +<br> |
| 53 | +<p><img src="/images/info.svg" alt="info" /> See also my <a href="https://github.com/learnbyexample/learn_gnused">GNU sed</a> ebook.</p> |
| 54 | +</content> |
| 55 | + </entry> |
10 | 56 | <entry xml:lang="en">
|
11 | 57 | <title>Computing from the Command Line book announcement</title>
|
12 | 58 | <published>2022-11-01T00:00:00+00:00</published>
|
|
0 commit comments