Skip to content

Commit ef87ff9

Browse files
added 28th cli tip
1 parent 3f74a8a commit ef87ff9

File tree

10 files changed

+79
-8
lines changed

10 files changed

+79
-8
lines changed

Diff for: README.md

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

6969
## Tips
7070

71+
* [CLI tip 28: substitute specific occurrence with GNU sed](https://learnbyexample.github.io/tips/cli-tip-28/)
7172
* [Python tip 28: string concatenation and repetition](https://learnbyexample.github.io/tips/python-tip-28/)
7273
* [Vim tip 26: executing shell commands](https://learnbyexample.github.io/tips/vim-tip-26/)
7374
* [CLI tip 27: reverse text line wise with tac](https://learnbyexample.github.io/tips/cli-tip-27/)

Diff for: atom.xml

+43-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,50 @@
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-05-24T00:00:00+00:00</updated>
8+
<updated>2023-05-30T00:00:00+00:00</updated>
99
<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">&lt;p&gt;By using the &lt;code&gt;g&lt;&#x2F;code&gt; flag with the &lt;code&gt;s&lt;&#x2F;code&gt; command (substitute), you can search and replace all occurrences of a pattern. Without the &lt;code&gt;g&lt;&#x2F;code&gt; flag, only the first matching portion will be replaced.&lt;&#x2F;p&gt;
17+
&lt;p&gt;Did you know that you can use a number as a flag to replace only that particular occurrence of matching parts?&lt;&#x2F;p&gt;
18+
&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 style=&quot;color:#7f8989;&quot;&gt;# replace only the third occurrence of &amp;#39;:&amp;#39; with &amp;#39;---&amp;#39;
19+
&lt;&#x2F;span&gt;&lt;span&gt;$ echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;apple:banana:cherry:fig:mango&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; sed &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;s&#x2F;:&#x2F;---&#x2F;3&amp;#39;
20+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;apple:banana:&lt;&#x2F;span&gt;&lt;span&gt;cherry&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;---&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;fig:&lt;&#x2F;span&gt;&lt;span&gt;mango
21+
&lt;&#x2F;span&gt;&lt;span&gt;
22+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#7f8989;&quot;&gt;# replace only the second occurrence of a word starting with &amp;#39;t&amp;#39;
23+
&lt;&#x2F;span&gt;&lt;span&gt;$ echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;book table bus car banana tap camp&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; sed &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;s&#x2F;\bt\w*&#x2F;&amp;quot;&amp;amp;&amp;quot;&#x2F;2&amp;#39;
24+
&lt;&#x2F;span&gt;&lt;span&gt;book table bus car banana &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;quot;tap&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt; camp
25+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
26+
&lt;p&gt;To replace a specific occurrence from the end of the line, you&#x27;ll have use regular expression tricks:&lt;&#x2F;p&gt;
27+
&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;$ s=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;apple:banana:cherry:fig:mango&amp;#39;
28+
&lt;&#x2F;span&gt;&lt;span&gt;
29+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#7f8989;&quot;&gt;# replace the last occurrence
30+
&lt;&#x2F;span&gt;&lt;span&gt;$ echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;quot;$s&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; sed &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;E &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;s&#x2F;(.*):&#x2F;\1---&#x2F;&amp;#39;
31+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;apple:banana:cherry:&lt;&#x2F;span&gt;&lt;span&gt;fig&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;---&lt;&#x2F;span&gt;&lt;span&gt;mango
32+
&lt;&#x2F;span&gt;&lt;span&gt;
33+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#7f8989;&quot;&gt;# replace the last but one occurrence
34+
&lt;&#x2F;span&gt;&lt;span&gt;$ echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;quot;$s&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; sed &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;E &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;s&#x2F;(.*):(.*:)&#x2F;\1---\2&#x2F;&amp;#39;
35+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;apple:banana:&lt;&#x2F;span&gt;&lt;span&gt;cherry&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;---&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;fig:&lt;&#x2F;span&gt;&lt;span&gt;mango
36+
&lt;&#x2F;span&gt;&lt;span&gt;
37+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#7f8989;&quot;&gt;# generic formula, where {N} refers to the last but Nth occurrence
38+
&lt;&#x2F;span&gt;&lt;span&gt;$ echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;quot;$s&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; sed &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;E &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;s&#x2F;(.*):((.*:){2})&#x2F;\1---\2&#x2F;&amp;#39;
39+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;apple:&lt;&#x2F;span&gt;&lt;span&gt;banana&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;---&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;cherry:fig:&lt;&#x2F;span&gt;&lt;span&gt;mango
40+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
41+
&lt;p&gt;If you combine a number flag with the &lt;code&gt;g&lt;&#x2F;code&gt; flag, all matches from that particular occurrence will be replaced.&lt;&#x2F;p&gt;
42+
&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 style=&quot;color:#7f8989;&quot;&gt;# replace except the first occurrence of a word starting with &amp;#39;b&amp;#39;
43+
&lt;&#x2F;span&gt;&lt;span&gt;$ echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;book table bus car banana tap camp&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; sed &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;s&#x2F;\bb\w*&#x2F;&amp;quot;&amp;amp;&amp;quot;&#x2F;2g&amp;#39;
44+
&lt;&#x2F;span&gt;&lt;span&gt;book table &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;quot;bus&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt; car &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;quot;banana&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b39f04;&quot;&gt;tap&lt;&#x2F;span&gt;&lt;span&gt; camp
45+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
46+
&lt;p&gt;&lt;strong&gt;Video demo&lt;&#x2F;strong&gt;:&lt;&#x2F;p&gt;
47+
&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;7CxaiYQ2gIU&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;
48+
&lt;br&gt;
49+
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;info.svg&quot; alt=&quot;info&quot; &#x2F;&gt; See also my &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;learnbyexample&#x2F;learn_gnused&quot;&gt;GNU sed&lt;&#x2F;a&gt; ebook.&lt;&#x2F;p&gt;
50+
</content>
51+
</entry>
1052
<entry xml:lang="en">
1153
<title>Python tip 28: string concatenation and repetition</title>
1254
<published>2023-05-24T00:00:00+00:00</published>

Diff for: sitemap.xml

+5-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@
428428
</url>
429429
<url>
430430
<loc>https://learnbyexample.github.io/tips/</loc>
431-
<lastmod>2023-05-24</lastmod>
431+
<lastmod>2023-05-30</lastmod>
432432
</url>
433433
<url>
434434
<loc>https://learnbyexample.github.io/tips/cli-tip-1/</loc>
@@ -510,6 +510,10 @@
510510
<loc>https://learnbyexample.github.io/tips/cli-tip-27/</loc>
511511
<lastmod>2023-05-09</lastmod>
512512
</url>
513+
<url>
514+
<loc>https://learnbyexample.github.io/tips/cli-tip-28/</loc>
515+
<lastmod>2023-05-30</lastmod>
516+
</url>
513517
<url>
514518
<loc>https://learnbyexample.github.io/tips/cli-tip-3/</loc>
515519
<lastmod>2022-06-14</lastmod>

0 commit comments

Comments
 (0)