Skip to content

Commit e25418e

Browse files
added twelfth CLI tip
1 parent f9c8159 commit e25418e

File tree

9 files changed

+445
-4
lines changed

9 files changed

+445
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ I'm addicted to reading fantasy/sci-fi books, so I have a [blog](https://learnby
5656

5757
## Tips
5858

59+
* [CLI tip 12: squeeze empty lines](https://learnbyexample.github.io/tips/cli-tip-12/)
5960
* [Python tip 12: negate a regex grouping](https://learnbyexample.github.io/tips/python-tip-12/)
6061
* [Vim tip 10: Undo and Redo](https://learnbyexample.github.io/tips/vim-tip-10/)
6162
* [CLI tip 11: longest line length](https://learnbyexample.github.io/tips/cli-tip-11/)

atom.xml

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,128 @@
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>2022-06-22T00:00:00+00:00</updated>
8+
<updated>2022-06-29T00:00:00+00:00</updated>
99
<id>https://learnbyexample.github.io/atom.xml</id>
10+
<entry xml:lang="en">
11+
<title>CLI tip 12: squeeze empty lines</title>
12+
<published>2022-06-29T00:00:00+00:00</published>
13+
<updated>2022-06-29T00:00:00+00:00</updated>
14+
<link href="https://learnbyexample.github.io/tips/cli-tip-12/" type="text/html"/>
15+
<id>https://learnbyexample.github.io/tips/cli-tip-12/</id>
16+
<content type="html">&lt;p&gt;&lt;code&gt;awk&lt;&#x2F;code&gt; has a builtin feature to process input content paragraph wise (by setting &lt;code&gt;RS&lt;&#x2F;code&gt; to an empty string). But, did you know that &lt;code&gt;cat&lt;&#x2F;code&gt;, &lt;code&gt;less&lt;&#x2F;code&gt; and &lt;code&gt;grep&lt;&#x2F;code&gt; can also be used to squeeze empty lines?&lt;&#x2F;p&gt;
17+
&lt;p&gt;&lt;code&gt;cat -s&lt;&#x2F;code&gt; (and &lt;code&gt;less -s&lt;&#x2F;code&gt;) will squeeze multiple empty lines in the input to a single empty line in the output. Here&#x27;s an example:&lt;&#x2F;p&gt;
18+
&lt;pre style=&quot;background-color:#f5f5f5;&quot;&gt;
19+
&lt;code&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;$ cat ip.txt
20+
hello
21+
22+
23+
24+
25+
world
26+
27+
apple
28+
banana
29+
cherry
30+
31+
32+
tea coffee
33+
chocolate
34+
$ cat &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;s ip.txt
35+
hello
36+
37+
world
38+
39+
apple
40+
banana
41+
cherry
42+
43+
tea coffee
44+
chocolate
45+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
46+
&lt;p&gt;Here&#x27;s an example with empty lines at the start&#x2F;end of the input:&lt;&#x2F;p&gt;
47+
&lt;pre style=&quot;background-color:#f5f5f5;&quot;&gt;
48+
&lt;code&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b39f04;&quot;&gt;printf &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;\n\n\ndragon\n\n\nunicorn\n\n\n&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;
49+
50+
51+
52+
dragon
53+
54+
55+
unicorn
56+
57+
58+
$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b39f04;&quot;&gt;printf &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;\n\n\ndragon\n\n\nunicorn\n\n\n&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt; cat &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;s
59+
60+
dragon
61+
62+
unicorn
63+
64+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
65+
&lt;p&gt;And here&#x27;s a solution with &lt;code&gt;awk&lt;&#x2F;code&gt;. Unlike the &lt;code&gt;-s&lt;&#x2F;code&gt; option, this will completely remove empty lines at the start&#x2F;end of the input.&lt;&#x2F;p&gt;
66+
&lt;pre style=&quot;background-color:#f5f5f5;&quot;&gt;
67+
&lt;code&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;$ awk &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;v &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c23f31;&quot;&gt;RS&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;{print s $0; s=&amp;quot;\n&amp;quot;}&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt; ip.txt
68+
hello
69+
70+
world
71+
72+
apple
73+
banana
74+
cherry
75+
76+
tea coffee
77+
chocolate
78+
79+
$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b39f04;&quot;&gt;printf &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;\n\n\ndragon\n\n\nunicorn\n\n\n&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt; awk &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;v &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c23f31;&quot;&gt;RS&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;{print s $0; s=&amp;quot;\n&amp;quot;}&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;
80+
dragon
81+
82+
unicorn
83+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
84+
&lt;p&gt;The &lt;code&gt;awk&lt;&#x2F;code&gt; solution would be easier to extend, given its programmable features. For example, two empty lines between the groups:&lt;&#x2F;p&gt;
85+
&lt;pre style=&quot;background-color:#f5f5f5;&quot;&gt;
86+
&lt;code&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;$ awk &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;v &lt;&#x2F;span&gt;&lt;span style=&quot;color:#c23f31;&quot;&gt;RS&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;{print s $0; s=&amp;quot;\n\n&amp;quot;}&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt; ip.txt
87+
hello
88+
89+
90+
world
91+
92+
93+
apple
94+
banana
95+
cherry
96+
97+
98+
tea coffee
99+
chocolate
100+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
101+
&lt;p&gt;And here&#x27;s a surprising &lt;code&gt;GNU grep&lt;&#x2F;code&gt; solution, with a customizable group separator:&lt;&#x2F;p&gt;
102+
&lt;pre style=&quot;background-color:#f5f5f5;&quot;&gt;
103+
&lt;code&gt;&lt;span style=&quot;color:#7f8989;&quot;&gt;# single empty line
104+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;$ grep &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;--&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;group&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;separator= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;A0 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;.&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt; ip.txt
105+
hello
106+
107+
world
108+
109+
apple
110+
banana
111+
cherry
112+
113+
tea coffee
114+
chocolate
115+
116+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#7f8989;&quot;&gt;# double empty line
117+
# empty lines at the start&#x2F;end of the input are removed too
118+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b39f04;&quot;&gt;printf &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;\n\n\ndragon\n\n\nunicorn\n\n\n&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt; grep &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;--&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;group&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;separator=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;$&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39; -A0 &amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;
119+
dragon
120+
121+
122+
unicorn
123+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
124+
&lt;p&gt;&lt;strong&gt;Video demo&lt;&#x2F;strong&gt;:&lt;&#x2F;p&gt;
125+
&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;DjNF2Sbwyxk&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;
126+
&lt;br&gt;
127+
&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;cli_text_processing_coreutils&quot;&gt;Command line text processing with GNU Coreutils&lt;&#x2F;a&gt;, &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;learnbyexample&#x2F;learn_gnuawk&quot;&gt;GNU AWK&lt;&#x2F;a&gt; and &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;learnbyexample&#x2F;learn_gnugrep_ripgrep&quot;&gt;GNU GREP and RIPGREP&lt;&#x2F;a&gt; ebooks.&lt;&#x2F;p&gt;
128+
</content>
129+
</entry>
10130
<entry xml:lang="en">
11131
<title>Python tip 12: negate a regex grouping</title>
12132
<published>2022-06-22T00:00:00+00:00</published>

sitemap.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,10 @@
382382
<loc>https://learnbyexample.github.io/tips/cli-tip-11/</loc>
383383
<lastmod>2022-06-08</lastmod>
384384
</url>
385+
<url>
386+
<loc>https://learnbyexample.github.io/tips/cli-tip-12/</loc>
387+
<lastmod>2022-06-29</lastmod>
388+
</url>
385389
<url>
386390
<loc>https://learnbyexample.github.io/tips/cli-tip-2/</loc>
387391
<lastmod>2022-05-27</lastmod>

tags/command-line/index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@
124124
<div class="taxonomy">
125125
<h2>command-line</h2>
126126

127+
<div class="taxonomy__item">
128+
<span class="taxonomy__item__time">2022-06-29</span>
129+
<span class="taxonomy__item__title">
130+
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tips&#x2F;cli-tip-12&#x2F;">CLI tip 12: squeeze empty lines</a>
131+
</span>
132+
</div>
133+
127134
<div class="taxonomy__item">
128135
<span class="taxonomy__item__time">2022-06-08</span>
129136
<span class="taxonomy__item__title">

tags/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@
164164

165165
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tags&#x2F;command-line&#x2F;">
166166
command-line
167-
<span class="count">24</span>
167+
<span class="count">25</span>
168168
</a>
169169

170170
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tags&#x2F;coreutils&#x2F;">
@@ -269,7 +269,7 @@
269269

270270
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tags&#x2F;linux&#x2F;">
271271
linux
272-
<span class="count">19</span>
272+
<span class="count">20</span>
273273
</a>
274274

275275
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tags&#x2F;lookarounds&#x2F;">
@@ -404,7 +404,7 @@
404404

405405
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tags&#x2F;tip&#x2F;">
406406
tip
407-
<span class="count">33</span>
407+
<span class="count">34</span>
408408
</a>
409409

410410
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tags&#x2F;tutorial&#x2F;">

tags/linux/index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@
124124
<div class="taxonomy">
125125
<h2>linux</h2>
126126

127+
<div class="taxonomy__item">
128+
<span class="taxonomy__item__time">2022-06-29</span>
129+
<span class="taxonomy__item__title">
130+
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tips&#x2F;cli-tip-12&#x2F;">CLI tip 12: squeeze empty lines</a>
131+
</span>
132+
</div>
133+
127134
<div class="taxonomy__item">
128135
<span class="taxonomy__item__time">2022-06-08</span>
129136
<span class="taxonomy__item__title">

tags/tip/index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@
124124
<div class="taxonomy">
125125
<h2>tip</h2>
126126

127+
<div class="taxonomy__item">
128+
<span class="taxonomy__item__time">2022-06-29</span>
129+
<span class="taxonomy__item__title">
130+
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tips&#x2F;cli-tip-12&#x2F;">CLI tip 12: squeeze empty lines</a>
131+
</span>
132+
</div>
133+
127134
<div class="taxonomy__item">
128135
<span class="taxonomy__item__time">2022-06-22</span>
129136
<span class="taxonomy__item__title">

0 commit comments

Comments
 (0)