<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>Posts on annotat.io</title>
		<link>https://annotat.io/posts/</link>
		<description>Recent content in Posts on annotat.io</description>
		<generator>Hugo -- gohugo.io</generator>
		<language>en-us</language>
		<copyright>This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.</copyright>
		<lastBuildDate>Sat, 20 Jul 2019 10:50:00 +0200</lastBuildDate>
		<atom:link href="https://annotat.io/posts/index.xml" rel="self" type="application/rss+xml" />
		
		<item>
			<title>Command line utilities I didn&#39;t know of: `paste`</title>
			<link>https://annotat.io/posts/paste_command/</link>
			<pubDate>Sat, 20 Jul 2019 10:50:00 +0200</pubDate>
			
			<guid>https://annotat.io/posts/paste_command/</guid>
			<description>Have you ever had two lists and you wanted to print their elements side by side? Well, if that&amp;rsquo;s the case, then the paste command is for you.
In fact, it&amp;rsquo;s the opposite of the cat command: While cat concatenates the lines of multiple files vertically (i.e. sequentally), paste assembles them horizontally. For instance, you have two lists:
# one.list 1 2 3 # two.list a b c With paste one.</description>
			<content type="html"><![CDATA[<p>Have you ever had two lists and you wanted to print their elements side by side? Well, if that&rsquo;s the case, then the <code>paste</code> command is for you.</p>
<!-- raw HTML omitted -->
<p>In fact, it&rsquo;s the opposite of the <code>cat</code> command: While <code>cat</code> concatenates the lines of multiple files <em>vertically</em> (i.e. sequentally), <code>paste</code> assembles them <em>horizontally</em>. For instance, you have two lists:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"><span style="font-style:italic"># one.list</span>
1
2
3
</code></pre></div><div class="highlight"><pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"><span style="font-style:italic"># two.list</span>
a
b
c
</code></pre></div><p>With <code>paste one.list two.list</code>, you get this:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">1   a
2   b
3   c
</code></pre></div><p>Apart from that, there are few options: With the <code>-d</code> flag you can set another delimiter than the default tab. <code>-s</code> transposes the result:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"><span style="font-style:italic"># paste -s one.list two.list</span>
1   2   3
a   b   c
</code></pre></div><p>Simple but useful.</p>
]]></content>
		</item>
		
		<item>
			<title>Pipe to multiple commands</title>
			<link>https://annotat.io/posts/pipe-to-multiple-commands/</link>
			<pubDate>Mon, 01 Apr 2019 22:00:00 +0200</pubDate>
			
			<guid>https://annotat.io/posts/pipe-to-multiple-commands/</guid>
			<description>&lt;p&gt;Building pipelines with different commands is bread and butter for every Unix / Linux shell user. But what if you want to reuse the output of one command in multiple others?&lt;/p&gt;</description>
			<content type="html"><![CDATA[<p>Building pipelines with different commands is bread and butter for every Unix / Linux shell user. But what if you want to reuse the output of one command in multiple others?</p>
<h2 id="pipes">Pipes</h2>
<p>The arguably most famous element of the <a href="https://en.wikipedia.org/wiki/Unix_philosophy">Unix philosophy</a> is the
demand to</p>
<blockquote>
<p>write program that do one thing and do it well.</p>
</blockquote>
<p>Complex applications can build on these simple components and stick them together to match their respective needs. But in order to do so, the building blocks have to communicate over a very generic interface. This is where pipes
come in, connecting standard output (<code>stdout</code>) of one component to standard input (<code>stdin</code>) of another component. I won&rsquo;t go into details here<sup id="fnref:1"><a href="#fn:1" class="footnote-ref" role="doc-noteref">1</a></sup>, but the important point is that normally you have one command which generates some content and sends it via pipe to the next command down the line. For instance</p>
<div class="highlight"><pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">cat my_lengthy_treatise_on_unix | grep unix
</code></pre></div><p><code>cat</code> echoes the content of a file (here <code>my_lengthy_treatise_on_unix</code>) while <code>grep</code> scans the input for a certain pattern (here simply <code>unix</code>) and outputs matching lines.</p>
<h2 id="tee">Tee</h2>
<p>This is a very common pattern in Unix shell scripts. But it gets more complex if you want apply multiple operations on the same input. For example, you intend to analyze the content of the text file mentioned before filtering it by several patterns. But instead of concatenating the matching lines, you want to print the matches to each pattern into a separate file:</p>
<pre tabindex="0"><code>              +----------------+     +---------+
    +--------&gt;+ grep pattern 1 +----&gt;+ output1 |
    |         +----------------+     +---------+
+---+---+
| Input |
+---+---+
    |         +----------------+     +---------+
    +--------&gt;+ grep pattern2  +----&gt;+ output2 |
              +----------------+     +---------+

</code></pre><p>There is a command you can use for this goal: <code>tee</code>. As its man page states:</p>
<blockquote>
<p>read from standard input and write to standard output and files</p>
</blockquote>
<p>And that&rsquo;s almost all there is to it. But mind the plural form of &ldquo;files&rdquo;. So <code>tee</code> allows to send <code>stdin</code> to as much files as you want and at last to <code>stdout</code>. Since there is only one <code>stdout</code>, it of no interest for us. So we can get rid of it by writing it to <code>/dev/null</code>:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">cat input | tee file1 file2 &gt;/dev/null
</code></pre></div><p>Unfortunately the redirection to files isn&rsquo;t either what we really want, because we need to further process the streaming data instead of just dumping it to files. But what if we could &ldquo;disguise&rdquo; commands as files? That&rsquo;s where <strong>process substitution</strong> comes into play.</p>
<h2 id="process-substitution">Process substitution</h2>
<p>Process substitution<sup id="fnref:2"><a href="#fn:2" class="footnote-ref" role="doc-noteref">2</a></sup> is a mechanism that some shells (like Bash, Zsh or the Korn shell) use to trick commands into thinking that they communicate with a file (either as input or ouput), but which is in reality replaced by the shell with a command or a pipeline of commands. The syntax is as follows:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">command_reading_data &lt;(command_producing_input)   <span style="font-style:italic"># Process substitution for input</span>
command_writing_data &gt;(command_processing_output) <span style="font-style:italic"># Process substitution for output</span>
</code></pre></div><p>A quite common use case is to normalize to data sources before comparing them:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">diff &lt;(sort file1) &lt;(sort file2)
</code></pre></div><p>As you might guess by now, we use process substitution for the searching and dumping part in our example. And so we end up with the following solution:</p>
<div class="highlight"><pre tabindex="0" style="background-color:#fff;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash">cat input | tee &gt;(grep <span style="font-style:italic">&#34;pattern1&#34;</span> &gt;output_file_1) &gt;(grep &gt;output_file_2) &gt;/dev/null
</code></pre></div><section class="footnotes" role="doc-endnotes">
<hr>
<ol>
<li id="fn:1" role="doc-endnote">
<p><a href="https://en.wikipedia.org/wiki/Anonymous_pipe">Wikipedia</a> gives an overview of the inner workings of pipes&#160;<a href="#fnref:1" class="footnote-backref" role="doc-backlink">&#x21a9;&#xfe0e;</a></p>
</li>
<li id="fn:2" role="doc-endnote">
<p>For a lot more examples see the <a href="https://www.tldp.org/LDP/abs/html/process-sub.html">respective chapter</a> in the <a href="https://www.tldp.org/LDP/abs/html/index.html">Advanced Bash-scripting Guide</a>&#160;<a href="#fnref:2" class="footnote-backref" role="doc-backlink">&#x21a9;&#xfe0e;</a></p>
</li>
</ol>
</section>]]></content>
		</item>
		
	</channel>
</rss>
