|
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-05-24T00:00:00+00:00</updated> |
| 8 | + <updated>2023-05-30T00:00:00+00:00</updated> |
9 | 9 | <id>https://learnbyexample.github.io/atom.xml</id>
|
| 10 | + <entry xml:lang="en"> |
| 11 | + <title>CLI tip 28: substitute specific occurrence with GNU sed</title> |
| 12 | + <published>2023-05-30T00:00:00+00:00</published> |
| 13 | + <updated>2023-05-30T00:00:00+00:00</updated> |
| 14 | + <link rel="alternate" href="https://learnbyexample.github.io/tips/cli-tip-28/" type="text/html"/> |
| 15 | + <id>https://learnbyexample.github.io/tips/cli-tip-28/</id> |
| 16 | + <content type="html"><p>By using the <code>g</code> flag with the <code>s</code> command (substitute), you can search and replace all occurrences of a pattern. Without the <code>g</code> flag, only the first matching portion will be replaced.</p> |
| 17 | +<p>Did you know that you can use a number as a flag to replace only that particular occurrence of matching parts?</p> |
| 18 | +<pre data-lang="ruby" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-ruby "><code class="language-ruby" data-lang="ruby"><span style="color:#7f8989;"># replace only the third occurrence of &#39;:&#39; with &#39;---&#39; |
| 19 | +</span><span>$ echo </span><span style="color:#d07711;">&#39;apple:banana:cherry:fig:mango&#39; </span><span style="color:#72ab00;">|</span><span> sed </span><span style="color:#d07711;">&#39;s/:/---/3&#39; |
| 20 | +</span><span style="color:#b3933a;">apple:banana:</span><span>cherry</span><span style="color:#72ab00;">---</span><span style="color:#b3933a;">fig:</span><span>mango |
| 21 | +</span><span> |
| 22 | +</span><span style="color:#7f8989;"># replace only the second occurrence of a word starting with &#39;t&#39; |
| 23 | +</span><span>$ echo </span><span style="color:#d07711;">&#39;book table bus car banana tap camp&#39; </span><span style="color:#72ab00;">|</span><span> sed </span><span style="color:#d07711;">&#39;s/\bt\w*/&quot;&amp;&quot;/2&#39; |
| 24 | +</span><span>book table bus car banana </span><span style="color:#d07711;">&quot;tap&quot;</span><span> camp |
| 25 | +</span></code></pre> |
| 26 | +<p>To replace a specific occurrence from the end of the line, you'll have use regular expression tricks:</p> |
| 27 | +<pre data-lang="ruby" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-ruby "><code class="language-ruby" data-lang="ruby"><span>$ s=</span><span style="color:#d07711;">&#39;apple:banana:cherry:fig:mango&#39; |
| 28 | +</span><span> |
| 29 | +</span><span style="color:#7f8989;"># replace the last occurrence |
| 30 | +</span><span>$ echo </span><span style="color:#d07711;">&quot;$s&quot; </span><span style="color:#72ab00;">|</span><span> sed </span><span style="color:#72ab00;">-</span><span style="color:#5597d6;">E </span><span style="color:#d07711;">&#39;s/(.*):/\1---/&#39; |
| 31 | +</span><span style="color:#b3933a;">apple:banana:cherry:</span><span>fig</span><span style="color:#72ab00;">---</span><span>mango |
| 32 | +</span><span> |
| 33 | +</span><span style="color:#7f8989;"># replace the last but one occurrence |
| 34 | +</span><span>$ echo </span><span style="color:#d07711;">&quot;$s&quot; </span><span style="color:#72ab00;">|</span><span> sed </span><span style="color:#72ab00;">-</span><span style="color:#5597d6;">E </span><span style="color:#d07711;">&#39;s/(.*):(.*:)/\1---\2/&#39; |
| 35 | +</span><span style="color:#b3933a;">apple:banana:</span><span>cherry</span><span style="color:#72ab00;">---</span><span style="color:#b3933a;">fig:</span><span>mango |
| 36 | +</span><span> |
| 37 | +</span><span style="color:#7f8989;"># generic formula, where {N} refers to the last but Nth occurrence |
| 38 | +</span><span>$ echo </span><span style="color:#d07711;">&quot;$s&quot; </span><span style="color:#72ab00;">|</span><span> sed </span><span style="color:#72ab00;">-</span><span style="color:#5597d6;">E </span><span style="color:#d07711;">&#39;s/(.*):((.*:){2})/\1---\2/&#39; |
| 39 | +</span><span style="color:#b3933a;">apple:</span><span>banana</span><span style="color:#72ab00;">---</span><span style="color:#b3933a;">cherry:fig:</span><span>mango |
| 40 | +</span></code></pre> |
| 41 | +<p>If you combine a number flag with the <code>g</code> flag, all matches from that particular occurrence will be replaced.</p> |
| 42 | +<pre data-lang="ruby" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-ruby "><code class="language-ruby" data-lang="ruby"><span style="color:#7f8989;"># replace except the first occurrence of a word starting with &#39;b&#39; |
| 43 | +</span><span>$ echo </span><span style="color:#d07711;">&#39;book table bus car banana tap camp&#39; </span><span style="color:#72ab00;">|</span><span> sed </span><span style="color:#d07711;">&#39;s/\bb\w*/&quot;&amp;&quot;/2g&#39; |
| 44 | +</span><span>book table </span><span style="color:#d07711;">&quot;bus&quot;</span><span> car </span><span style="color:#d07711;">&quot;banana&quot; </span><span style="color:#b39f04;">tap</span><span> camp |
| 45 | +</span></code></pre> |
| 46 | +<p><strong>Video demo</strong>:</p> |
| 47 | +<p align="center"><iframe width="560" height="315" loading="lazy" src="https://www.youtube.com/embed/7CxaiYQ2gIU" title="YouTube video player" frameborder="0" allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p> |
| 48 | +<br> |
| 49 | +<p><img src="/images/info.svg" alt="info" /> See also my <a href="https://github.com/learnbyexample/learn_gnused">GNU sed</a> ebook.</p> |
| 50 | +</content> |
| 51 | + </entry> |
10 | 52 | <entry xml:lang="en">
|
11 | 53 | <title>Python tip 28: string concatenation and repetition</title>
|
12 | 54 | <published>2023-05-24T00:00:00+00:00</published>
|
|
0 commit comments