|
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>2022-12-09T00:00:00+00:00</updated> |
| 8 | + <updated>2022-12-14T00:00:00+00:00</updated> |
9 | 9 | <id>https://learnbyexample.github.io/atom.xml</id>
|
| 10 | + <entry xml:lang="en"> |
| 11 | + <title>CLI tip 20: expand and unexpand</title> |
| 12 | + <published>2022-12-14T00:00:00+00:00</published> |
| 13 | + <updated>2022-12-14T00:00:00+00:00</updated> |
| 14 | + <link rel="alternate" href="https://learnbyexample.github.io/tips/cli-tip-20/" type="text/html"/> |
| 15 | + <id>https://learnbyexample.github.io/tips/cli-tip-20/</id> |
| 16 | + <content type="html"><p>These two commands will help you convert tabs to spaces and vice versa. Both these commands support options to customize the width of tab stops and which occurrences should be converted.</p> |
| 17 | +<p>The default expansion aligns at multiples of <code>8</code> columns (calculated in terms of bytes).</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;"># &#39;apple&#39; = 5 bytes, \t converts to 3 spaces |
| 19 | +</span><span style="color:#7f8989;"># &#39;banana&#39; = 6 bytes, \t converts to 2 spaces |
| 20 | +</span><span style="color:#7f8989;"># &#39;a&#39; and &#39;b&#39; = 1 byte, \t converts to 7 spaces |
| 21 | +</span><span>$ </span><span style="color:#b39f04;">printf </span><span style="color:#d07711;">&#39;apple\tbanana\tcherry\na\tb\tc\n&#39; </span><span style="color:#72ab00;">|</span><span> expand |
| 22 | +</span><span>apple banana cherry |
| 23 | +</span><span>a b c |
| 24 | +</span><span> |
| 25 | +</span><span style="color:#7f8989;"># &#39;αλε&#39; = 6 bytes, \t converts to 2 spaces |
| 26 | +</span><span>$ </span><span style="color:#b39f04;">printf </span><span style="color:#d07711;">&#39;αλε\tπού\n&#39; </span><span style="color:#72ab00;">|</span><span> expand |
| 27 | +</span><span>αλε πού |
| 28 | +</span></code></pre> |
| 29 | +<p>By default, the <code>unexpand</code> command converts initial blank (space or tab) characters to tabs. The first occurrence of a non-blank character will stop the conversion. By default, every <code>8</code> columns worth of blanks is converted to a tab.</p> |
| 30 | +<pre data-lang="ruby" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-ruby "><code class="language-ruby" data-lang="ruby"><span style="color:#7f8989;"># input is 8 spaces followed by &#39;a&#39; and then more characters |
| 31 | +</span><span style="color:#7f8989;"># the initial 8 spaces is converted to a tab character |
| 32 | +</span><span style="color:#7f8989;"># &#39;a&#39; stops any further conversion, since it is a non-blank character |
| 33 | +</span><span>$ </span><span style="color:#b39f04;">printf </span><span style="color:#d07711;">&#39; a b c\n&#39; </span><span style="color:#72ab00;">|</span><span> unexpand </span><span style="color:#72ab00;">|</span><span> cat </span><span style="color:#72ab00;">-</span><span style="color:#5597d6;">T |
| 34 | +</span><span style="color:#72ab00;">^</span><span style="color:#5597d6;">Ia</span><span> b c |
| 35 | +</span><span> |
| 36 | +</span><span style="color:#7f8989;"># input is 9 spaces followed by &#39;a&#39; and then more characters |
| 37 | +</span><span style="color:#7f8989;"># the initial 8 spaces is converted to a tab character |
| 38 | +</span><span style="color:#7f8989;"># remaining space is left as is |
| 39 | +</span><span>$ </span><span style="color:#b39f04;">printf </span><span style="color:#d07711;">&#39; a b c\n&#39; </span><span style="color:#72ab00;">|</span><span> unexpand </span><span style="color:#72ab00;">|</span><span> cat </span><span style="color:#72ab00;">-</span><span style="color:#5597d6;">T |
| 40 | +</span><span style="color:#72ab00;">^</span><span style="color:#5597d6;">I</span><span> a b c |
| 41 | +</span></code></pre> |
| 42 | +<p><strong>Video demo</strong>:</p> |
| 43 | +<p align="center"><iframe width="560" height="315" loading="lazy" src="https://www.youtube.com/embed/tsJCtE6oZDs" title="YouTube video player" frameborder="0" allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p> |
| 44 | +<br> |
| 45 | +<p><img src="/images/info.svg" alt="info" /> See <a href="https://learnbyexample.github.io/cli_text_processing_coreutils/expand-unexpand.html">expand and unexpand</a> chapter my <a href="https://github.com/learnbyexample/cli_text_processing_coreutils">Command line text processing with GNU Coreutils</a> ebook for more examples, options, etc.</p> |
| 46 | +</content> |
| 47 | + </entry> |
10 | 48 | <entry xml:lang="en">
|
11 | 49 | <title>Interactive Linux CLI Text Processing Exercises</title>
|
12 | 50 | <published>2022-12-09T00:00:00+00:00</published>
|
|
2888 | 2926 | <entry xml:lang="en">
|
2889 | 2927 | <title>Brag post: Hacker News Front Page entries</title>
|
2890 | 2928 | <published>2022-01-21T00:00:00+00:00</published>
|
2891 |
| - <updated>2022-03-16T00:00:00+00:00</updated> |
| 2929 | + <updated>2022-12-14T00:00:00+00:00</updated> |
2892 | 2930 | <link rel="alternate" href="https://learnbyexample.github.io/mini/hacker-news-front-page-brag/" type="text/html"/>
|
2893 | 2931 | <id>https://learnbyexample.github.io/mini/hacker-news-front-page-brag/</id>
|
2894 | 2932 | <content type="html"><p>In case you haven't yet read this nice post <a href="https://jvns.ca/blog/brag-documents/">&quot;Get your work recognized: write a brag document&quot;</a> by Julia Evans, please do that first.</p>
|
|
2907 | 2945 | <li><a href="https://news.ycombinator.com/item?id=29391107">Show HN: Improve your Python regex skills with 75 interactive exercises</a> — <em>175 points, Nov 2021, 12 comments</em></li>
|
2908 | 2946 | <li><a href="https://news.ycombinator.com/item?id=29837543">Vim prank: alias vim='vim -y'</a> — <em>341 points, Jan 2022, 259 comments</em></li>
|
2909 | 2947 | <li><a href="https://news.ycombinator.com/item?id=30684232">Vim Reference Guide</a> — <em>244 points, Mar 2022, 110 comments</em></li>
|
| 2948 | +<li><a href="https://news.ycombinator.com/item?id=33931677">Show HN: Interactive exercises for Linux CLI text processing commands</a> — <em>68 points, Dec 2022, 7 comments</em></li> |
2910 | 2949 | </ol>
|
2911 | 2950 | <p>As is the case with other social media platforms, being an active participant on Hacker News definitely helps. Apart from commenting on other topics, I also post links to projects and resources that I felt were useful. The number of such links reaching front page outnumbers my own content links.</p>
|
2912 | 2951 | </content>
|
|
0 commit comments