Skip to content

Commit a230a30

Browse files
added 27th cli tip
1 parent 3e41217 commit a230a30

File tree

10 files changed

+87
-8
lines changed

10 files changed

+87
-8
lines changed

README.md

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

6868
## Tips
6969

70+
* [CLI tip 27: reverse text line wise with tac](https://learnbyexample.github.io/tips/cli-tip-27/)
7071
* [Python tip 27: enumerate() function](https://learnbyexample.github.io/tips/python-tip-27/)
7172
* [Vim tip 25: substitute flags](https://learnbyexample.github.io/tips/vim-tip-25/)
7273
* [CLI tip 26: removing duplicate lines with GNU awk](https://learnbyexample.github.io/tips/cli-tip-26/)

atom.xml

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,53 @@
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-03T00:00:00+00:00</updated>
8+
<updated>2023-05-09T00:00:00+00:00</updated>
99
<id>https://learnbyexample.github.io/atom.xml</id>
10+
<entry xml:lang="en">
11+
<title>CLI tip 27: reverse text line wise with tac</title>
12+
<published>2023-05-09T00:00:00+00:00</published>
13+
<updated>2023-05-09T00:00:00+00:00</updated>
14+
<link rel="alternate" href="https://learnbyexample.github.io/tips/cli-tip-27/" type="text/html"/>
15+
<id>https://learnbyexample.github.io/tips/cli-tip-27/</id>
16+
<content type="html">&lt;p&gt;You can use &lt;code&gt;tac&lt;&#x2F;code&gt; to reverse the input line wise. If you pass multiple input files, each file content will be reversed separately.&lt;&#x2F;p&gt;
17+
&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;$ &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;apple\nbanana\ncherry\nfig and honey\n&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; tac
18+
&lt;&#x2F;span&gt;&lt;span&gt;fig &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;and&lt;&#x2F;span&gt;&lt;span&gt; honey
19+
&lt;&#x2F;span&gt;&lt;span&gt;cherry
20+
&lt;&#x2F;span&gt;&lt;span&gt;banana
21+
&lt;&#x2F;span&gt;&lt;span&gt;apple
22+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
23+
&lt;p&gt;You can use the &lt;code&gt;-s&lt;&#x2F;code&gt; option to specify a different string to be used as the &lt;em&gt;line&lt;&#x2F;em&gt; separator (newline is the default separator). When the custom separator occurs before the content of interest, use the &lt;code&gt;-b&lt;&#x2F;code&gt; option to print those separators before the content in the output as well.&lt;&#x2F;p&gt;
24+
&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;$ cat blocks.txt
25+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;%=%=
26+
&lt;&#x2F;span&gt;&lt;span&gt;apple
27+
&lt;&#x2F;span&gt;&lt;span&gt;banana
28+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;%=%=
29+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;1
30+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;2
31+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;3
32+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;%=%=
33+
&lt;&#x2F;span&gt;&lt;span&gt;red
34+
&lt;&#x2F;span&gt;&lt;span&gt;green
35+
&lt;&#x2F;span&gt;&lt;span&gt;
36+
&lt;&#x2F;span&gt;&lt;span&gt;$ tac &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;b &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;s &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;%=%=&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt; blocks.txt
37+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;%=%=
38+
&lt;&#x2F;span&gt;&lt;span&gt;red
39+
&lt;&#x2F;span&gt;&lt;span&gt;green
40+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;%=%=
41+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;1
42+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;2
43+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;3
44+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;%=%=
45+
&lt;&#x2F;span&gt;&lt;span&gt;apple
46+
&lt;&#x2F;span&gt;&lt;span&gt;banana
47+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
48+
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;info.svg&quot; alt=&quot;info&quot; &#x2F;&gt; See &lt;a href=&quot;https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tips&#x2F;cli-tip-8&#x2F;&quot;&gt;CLI tip 8: extract from start of file until matching line&lt;&#x2F;a&gt; for a practical example where reversing input content helps in constructing a solution.&lt;&#x2F;p&gt;
49+
&lt;p&gt;&lt;strong&gt;Video demo&lt;&#x2F;strong&gt;:&lt;&#x2F;p&gt;
50+
&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;6p_9zP66wqA&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;
51+
&lt;br&gt;
52+
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;info.svg&quot; alt=&quot;info&quot; &#x2F;&gt; See also &lt;a href=&quot;https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;cli_text_processing_coreutils&#x2F;cat-tac.html#tac&quot;&gt;tac&lt;&#x2F;a&gt; section from 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; ebook for more details and examples.&lt;&#x2F;p&gt;
53+
</content>
54+
</entry>
1055
<entry xml:lang="en">
1156
<title>Python tip 27: enumerate() function</title>
1257
<published>2023-05-03T00:00:00+00:00</published>

sitemap.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@
424424
</url>
425425
<url>
426426
<loc>https://learnbyexample.github.io/tips/</loc>
427-
<lastmod>2023-05-03</lastmod>
427+
<lastmod>2023-05-09</lastmod>
428428
</url>
429429
<url>
430430
<loc>https://learnbyexample.github.io/tips/cli-tip-1/</loc>
@@ -502,6 +502,10 @@
502502
<loc>https://learnbyexample.github.io/tips/cli-tip-26/</loc>
503503
<lastmod>2023-04-20</lastmod>
504504
</url>
505+
<url>
506+
<loc>https://learnbyexample.github.io/tips/cli-tip-27/</loc>
507+
<lastmod>2023-05-09</lastmod>
508+
</url>
505509
<url>
506510
<loc>https://learnbyexample.github.io/tips/cli-tip-3/</loc>
507511
<lastmod>2022-06-14</lastmod>

tags/command-line/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tags/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tags/linux/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tags/tip/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tips/cli-tip-26/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@
4848
</span><span>yellow,toy,flower,</span><span style=color:#b3933a;>333
4949
</span><span>white,sky,bread,</span><span style=color:#b3933a;>111
5050
</span><span>light red,purse,rose,</span><span style=color:#b3933a;>333
51-
</span></code></pre><p><img alt=info src=/images/info.svg> <a href=https://github.com/koraa/huniq>huniq</a> is a faster alternative for removing line based duplicates.<p><strong>Video demo</strong>:<p align=center><iframe allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" title="YouTube video player" allowfullscreen frameborder=0 height=315 loading=lazy src=https://www.youtube.com/embed/nfQn6IkxxeU width=560></iframe></p><br><p><img alt=info src=/images/info.svg> See also my <a href=https://github.com/learnbyexample/learn_gnuawk>GNU AWK</a> ebook.</div><div class=post-footer><div class=post-tags><a href=https://learnbyexample.github.io/tags/linux/>#linux</a><a href=https://learnbyexample.github.io/tags/command-line/>#command-line</a><a href=https://learnbyexample.github.io/tags/tip/>#tip</a></div><hr color=#e6e6e6><div class=post-nav><p><a class=next href=https://learnbyexample.github.io/tips/cli-tip-25/>CLI tip 25: get file properties using the stat command →</a><br></div><hr color=#e6e6e6><p>📰 Use <a href=https://learnbyexample.github.io/atom.xml>this link</a> for the Atom feed. <br> ✅ Follow me on <a href=https://twitter.com/learn_byexample>Twitter</a>, <a href=https://github.com/learnbyexample>GitHub</a> and <a href=https://www.youtube.com/c/learnbyexample42>Youtube</a> for interesting tech nuggets. <br> 📧 Subscribe to <a href=https://learnbyexample.gumroad.com/l/learnbyexample-weekly>learnbyexample weekly</a> for programming resources, tips, tools, free ebooks and more (free newsletter, delivered every Friday).<hr color=#e6e6e6></div></article></div></main></div><script src=https://learnbyexample.github.io/even.js></script>
51+
</span></code></pre><p><img alt=info src=/images/info.svg> <a href=https://github.com/koraa/huniq>huniq</a> is a faster alternative for removing line based duplicates.<p><strong>Video demo</strong>:<p align=center><iframe allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" title="YouTube video player" allowfullscreen frameborder=0 height=315 loading=lazy src=https://www.youtube.com/embed/nfQn6IkxxeU width=560></iframe></p><br><p><img alt=info src=/images/info.svg> See also my <a href=https://github.com/learnbyexample/learn_gnuawk>GNU AWK</a> ebook.</div><div class=post-footer><div class=post-tags><a href=https://learnbyexample.github.io/tags/linux/>#linux</a><a href=https://learnbyexample.github.io/tags/command-line/>#command-line</a><a href=https://learnbyexample.github.io/tags/tip/>#tip</a></div><hr color=#e6e6e6><div class=post-nav><p><a class=previous href=https://learnbyexample.github.io/tips/cli-tip-27/>← CLI tip 27: reverse text line wise with tac</a><br><p><a class=next href=https://learnbyexample.github.io/tips/cli-tip-25/>CLI tip 25: get file properties using the stat command →</a><br></div><hr color=#e6e6e6><p>📰 Use <a href=https://learnbyexample.github.io/atom.xml>this link</a> for the Atom feed. <br> ✅ Follow me on <a href=https://twitter.com/learn_byexample>Twitter</a>, <a href=https://github.com/learnbyexample>GitHub</a> and <a href=https://www.youtube.com/c/learnbyexample42>Youtube</a> for interesting tech nuggets. <br> 📧 Subscribe to <a href=https://learnbyexample.gumroad.com/l/learnbyexample-weekly>learnbyexample weekly</a> for programming resources, tips, tools, free ebooks and more (free newsletter, delivered every Friday).<hr color=#e6e6e6></div></article></div></main></div><script src=https://learnbyexample.github.io/even.js></script>

tips/cli-tip-27/index.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!doctype html><html lang=en><head><meta content="IE=edge" http-equiv=X-UA-Compatible><meta content="text/html; charset=utf-8" http-equiv=content-type><meta content="width=device-width,initial-scale=1.0,maximum-scale=1" name=viewport><title>CLI tip 27: reverse text line wise with tac</title><link href=https://learnbyexample.github.io/atom.xml rel=alternate title=RSS type=application/atom+xml><script src=https://cdnjs.cloudflare.com/ajax/libs/slideout/1.0.1/slideout.min.js></script><link href=https://learnbyexample.github.io/site.css rel=stylesheet><meta content=learnbyexample property=og:title><meta content=website property=og:type><meta content="Learn Python, Regex, Linux, Scripting, Vim, Ebooks, Self-Publishing and Interesting Tech Nuggets." property=og:description><meta content=https://learnbyexample.github.io property=og:url><meta content=https://learnbyexample.github.io/images/learnbyexample.png property=og:image><meta content=1280 property=og:image:width><meta content=640 property=og:image:height><meta content=summary_large_image property=twitter:card><meta content=@learn_byexample property=twitter:site><link href=https://learnbyexample.github.io/favicon.svg rel=icon><link rel="shortcut icon" href=https://learnbyexample.github.io/favicon.png><body><div class=container><div class=mobile-navbar id=mobile-navbar><div class=mobile-header-logo><a class=logo href=/>learnbyexample</a></div><div class="mobile-navbar-icon icon-out"><span></span><span></span><span></span></div></div><nav class="mobile-menu slideout-menu slideout-menu-left" id=mobile-menu><ul class=mobile-menu-list><li class=mobile-menu-item><a href=https://learnbyexample.github.io/books> Books </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/mini> Mini </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/tips> Tips </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/tags> Tags </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/about> About </a></ul></nav><header id=header><div class=logo><a href=https://learnbyexample.github.io>learnbyexample</a></div><nav class=menu><ul><li><a href=https://learnbyexample.github.io/books> Books </a><li><a href=https://learnbyexample.github.io/mini> Mini </a><li><a href=https://learnbyexample.github.io/tips> Tips </a><li><a href=https://learnbyexample.github.io/tags> Tags </a><li><a href=https://learnbyexample.github.io/about> About </a></ul></nav></header><main><div class=content id=mobile-panel><article class=post><header class=post__header><h1 class=post__title><a href=https://learnbyexample.github.io/tips/cli-tip-27/>CLI tip 27: reverse text line wise with tac</a></h1><div class=post__meta><span class=post__time>2023-05-09</span></div></header><div class=post-content><p>You can use <code>tac</code> to reverse the input line wise. If you pass multiple input files, each file content will be reversed separately.<pre class=language-ruby data-lang=ruby style=background-color:#f5f5f5;color:#1f1f1f;><code class=language-ruby data-lang=ruby><span>$ </span><span style=color:#b39f04;>printf </span><span style=color:#d07711;>'apple\nbanana\ncherry\nfig and honey\n' </span><span style=color:#72ab00;>|</span><span> tac
2+
</span><span>fig </span><span style=color:#72ab00;>and</span><span> honey
3+
</span><span>cherry
4+
</span><span>banana
5+
</span><span>apple
6+
</span></code></pre><p>You can use the <code>-s</code> option to specify a different string to be used as the <em>line</em> separator (newline is the default separator). When the custom separator occurs before the content of interest, use the <code>-b</code> option to print those separators before the content in the output as well.<pre class=language-ruby data-lang=ruby style=background-color:#f5f5f5;color:#1f1f1f;><code class=language-ruby data-lang=ruby><span>$ cat blocks.txt
7+
</span><span style=color:#72ab00;>%=%=
8+
</span><span>apple
9+
</span><span>banana
10+
</span><span style=color:#72ab00;>%=%=
11+
</span><span style=color:#b3933a;>1
12+
</span><span style=color:#b3933a;>2
13+
</span><span style=color:#b3933a;>3
14+
</span><span style=color:#72ab00;>%=%=
15+
</span><span>red
16+
</span><span>green
17+
</span><span>
18+
</span><span>$ tac </span><span style=color:#72ab00;>-</span><span>b </span><span style=color:#72ab00;>-</span><span>s </span><span style=color:#d07711;>'%=%='</span><span> blocks.txt
19+
</span><span style=color:#72ab00;>%=%=
20+
</span><span>red
21+
</span><span>green
22+
</span><span style=color:#72ab00;>%=%=
23+
</span><span style=color:#b3933a;>1
24+
</span><span style=color:#b3933a;>2
25+
</span><span style=color:#b3933a;>3
26+
</span><span style=color:#72ab00;>%=%=
27+
</span><span>apple
28+
</span><span>banana
29+
</span></code></pre><p><img alt=info src=/images/info.svg> See <a href=https://learnbyexample.github.io/tips/cli-tip-8/>CLI tip 8: extract from start of file until matching line</a> for a practical example where reversing input content helps in constructing a solution.<p><strong>Video demo</strong>:<p align=center><iframe allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" title="YouTube video player" allowfullscreen frameborder=0 height=315 loading=lazy src=https://www.youtube.com/embed/6p_9zP66wqA width=560></iframe></p><br><p><img alt=info src=/images/info.svg> See also <a href=https://learnbyexample.github.io/cli_text_processing_coreutils/cat-tac.html#tac>tac</a> section from my <a href=https://github.com/learnbyexample/cli_text_processing_coreutils>Command line text processing with GNU Coreutils</a> ebook for more details and examples.</div><div class=post-footer><div class=post-tags><a href=https://learnbyexample.github.io/tags/linux/>#linux</a><a href=https://learnbyexample.github.io/tags/command-line/>#command-line</a><a href=https://learnbyexample.github.io/tags/tip/>#tip</a></div><hr color=#e6e6e6><div class=post-nav><p><a class=next href=https://learnbyexample.github.io/tips/cli-tip-26/>CLI tip 26: removing duplicate lines with GNU awk →</a><br></div><hr color=#e6e6e6><p>📰 Use <a href=https://learnbyexample.github.io/atom.xml>this link</a> for the Atom feed. <br> ✅ Follow me on <a href=https://twitter.com/learn_byexample>Twitter</a>, <a href=https://github.com/learnbyexample>GitHub</a> and <a href=https://www.youtube.com/c/learnbyexample42>Youtube</a> for interesting tech nuggets. <br> 📧 Subscribe to <a href=https://learnbyexample.gumroad.com/l/learnbyexample-weekly>learnbyexample weekly</a> for programming resources, tips, tools, free ebooks and more (free newsletter, delivered every Friday).<hr color=#e6e6e6></div></article></div></main></div><script src=https://learnbyexample.github.io/even.js></script>

0 commit comments

Comments
 (0)