|
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-03T00:00:00+00:00</updated> |
| 8 | + <updated>2023-05-09T00:00:00+00:00</updated> |
9 | 9 | <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"><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.</p> |
| 17 | +<pre data-lang="ruby" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-ruby "><code class="language-ruby" data-lang="ruby"><span>$ </span><span style="color:#b39f04;">printf </span><span style="color:#d07711;">&#39;apple\nbanana\ncherry\nfig and honey\n&#39; </span><span style="color:#72ab00;">|</span><span> tac |
| 18 | +</span><span>fig </span><span style="color:#72ab00;">and</span><span> honey |
| 19 | +</span><span>cherry |
| 20 | +</span><span>banana |
| 21 | +</span><span>apple |
| 22 | +</span></code></pre> |
| 23 | +<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.</p> |
| 24 | +<pre data-lang="ruby" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-ruby "><code class="language-ruby" data-lang="ruby"><span>$ cat blocks.txt |
| 25 | +</span><span style="color:#72ab00;">%=%= |
| 26 | +</span><span>apple |
| 27 | +</span><span>banana |
| 28 | +</span><span style="color:#72ab00;">%=%= |
| 29 | +</span><span style="color:#b3933a;">1 |
| 30 | +</span><span style="color:#b3933a;">2 |
| 31 | +</span><span style="color:#b3933a;">3 |
| 32 | +</span><span style="color:#72ab00;">%=%= |
| 33 | +</span><span>red |
| 34 | +</span><span>green |
| 35 | +</span><span> |
| 36 | +</span><span>$ tac </span><span style="color:#72ab00;">-</span><span>b </span><span style="color:#72ab00;">-</span><span>s </span><span style="color:#d07711;">&#39;%=%=&#39;</span><span> blocks.txt |
| 37 | +</span><span style="color:#72ab00;">%=%= |
| 38 | +</span><span>red |
| 39 | +</span><span>green |
| 40 | +</span><span style="color:#72ab00;">%=%= |
| 41 | +</span><span style="color:#b3933a;">1 |
| 42 | +</span><span style="color:#b3933a;">2 |
| 43 | +</span><span style="color:#b3933a;">3 |
| 44 | +</span><span style="color:#72ab00;">%=%= |
| 45 | +</span><span>apple |
| 46 | +</span><span>banana |
| 47 | +</span></code></pre> |
| 48 | +<p><img src="/images/info.svg" alt="info" /> 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> |
| 49 | +<p><strong>Video demo</strong>:</p> |
| 50 | +<p align="center"><iframe width="560" height="315" loading="lazy" src="https://www.youtube.com/embed/6p_9zP66wqA" title="YouTube video player" frameborder="0" allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p> |
| 51 | +<br> |
| 52 | +<p><img src="/images/info.svg" alt="info" /> 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.</p> |
| 53 | +</content> |
| 54 | + </entry> |
10 | 55 | <entry xml:lang="en">
|
11 | 56 | <title>Python tip 27: enumerate() function</title>
|
12 | 57 | <published>2023-05-03T00:00:00+00:00</published>
|
|
0 commit comments