Skip to content

Commit 02fb1ca

Browse files
added 13th CLI tip
1 parent 85f1972 commit 02fb1ca

File tree

9 files changed

+279
-4
lines changed

9 files changed

+279
-4
lines changed

Diff for: README.md

+1
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 13: join lines of two files based on the first field](https://learnbyexample.github.io/tips/cli-tip-13/)
5960
* [Python tip 13: formatting numbers with underscore separation](https://learnbyexample.github.io/tips/python-tip-13/)
6061
* [Vim tip 11: replace characters in Normal mode](https://learnbyexample.github.io/tips/vim-tip-11/)
6162
* [CLI tip 12: squeeze empty lines](https://learnbyexample.github.io/tips/cli-tip-12/)

Diff for: atom.xml

+38-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,45 @@
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-07-13T00:00:00+00:00</updated>
8+
<updated>2022-07-20T00:00:00+00:00</updated>
99
<id>https://learnbyexample.github.io/atom.xml</id>
10+
<entry xml:lang="en">
11+
<title>CLI tip 13: join lines of two files based on the first field</title>
12+
<published>2022-07-20T00:00:00+00:00</published>
13+
<updated>2022-07-20T00:00:00+00:00</updated>
14+
<link href="https://learnbyexample.github.io/tips/cli-tip-13/" type="text/html"/>
15+
<id>https://learnbyexample.github.io/tips/cli-tip-13/</id>
16+
<content type="html">&lt;p&gt;By default, &lt;code&gt;join&lt;&#x2F;code&gt; combines two files based on the first field content (also referred as &lt;strong&gt;key&lt;&#x2F;strong&gt;). Only the lines with common keys will be part of the output. The key field will be displayed first in the output (this distinction will come into play if the first field isn&#x27;t the key). Rest of the line will have the remaining fields from the first and second files, in that order. One or more blanks (space or tab) will be considered as the input field separator and a single space will be used as the output field separator. If present, blank characters at the start of the input lines will be ignored.&lt;&#x2F;p&gt;
17+
&lt;pre style=&quot;background-color:#f5f5f5;&quot;&gt;
18+
&lt;code&gt;&lt;span style=&quot;color:#7f8989;&quot;&gt;# sample sorted input files
19+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;$ cat jan.txt
20+
apple &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;
21+
banana &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;20&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;
22+
soap &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;
23+
tshirt &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;
24+
$ cat feb.txt
25+
banana &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;15&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;
26+
fig &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;100&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;
27+
pen &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;
28+
soap &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;1
29+
30+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#7f8989;&quot;&gt;# combine common lines based on the first field
31+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;$ join jan.txt feb.txt
32+
banana &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;20 15&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;
33+
soap &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;3 1
34+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
35+
&lt;p&gt;Here&#x27;s an &lt;code&gt;awk&lt;&#x2F;code&gt; version to do the same. Helpful if you want to do some additional processing that won&#x27;t be possible with the &lt;code&gt;join&lt;&#x2F;code&gt; command. Another advantage is that this solution will work even if the input files are not sorted.&lt;&#x2F;p&gt;
36+
&lt;pre style=&quot;background-color:#f5f5f5;&quot;&gt;
37+
&lt;code&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;$ awk &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;NR==FNR{a[$1]=$2; next} $1 in a{print $1, a[$1], $2}&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt; jan.txt feb.txt
38+
banana &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;20 15&lt;&#x2F;span&gt;&lt;span style=&quot;color:#1f1f1f;&quot;&gt;
39+
soap &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;3 1
40+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
41+
&lt;p&gt;&lt;strong&gt;Video demo&lt;&#x2F;strong&gt;:&lt;&#x2F;p&gt;
42+
&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;Pjku4w7J6Zg&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;
43+
&lt;br&gt;
44+
&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;cli_text_processing_coreutils&#x2F;join.html&quot;&gt;join chapter&lt;&#x2F;a&gt; 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;
45+
</content>
46+
</entry>
1047
<entry xml:lang="en">
1148
<title>Python tip 13: formatting numbers with underscore separation</title>
1249
<published>2022-07-13T00:00:00+00:00</published>

Diff for: sitemap.xml

+4
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,10 @@
386386
<loc>https://learnbyexample.github.io/tips/cli-tip-12/</loc>
387387
<lastmod>2022-06-29</lastmod>
388388
</url>
389+
<url>
390+
<loc>https://learnbyexample.github.io/tips/cli-tip-13/</loc>
391+
<lastmod>2022-07-20</lastmod>
392+
</url>
389393
<url>
390394
<loc>https://learnbyexample.github.io/tips/cli-tip-2/</loc>
391395
<lastmod>2022-05-27</lastmod>

Diff for: tags/command-line/index.html

+7
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-07-20</span>
129+
<span class="taxonomy__item__title">
130+
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tips&#x2F;cli-tip-13&#x2F;">CLI tip 13: join lines of two files based on the first field</a>
131+
</span>
132+
</div>
133+
127134
<div class="taxonomy__item">
128135
<span class="taxonomy__item__time">2022-06-29</span>
129136
<span class="taxonomy__item__title">

Diff for: tags/index.html

+3-3
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">25</span>
167+
<span class="count">26</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">20</span>
272+
<span class="count">21</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">36</span>
407+
<span class="count">37</span>
408408
</a>
409409

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

Diff for: tags/linux/index.html

+7
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-07-20</span>
129+
<span class="taxonomy__item__title">
130+
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tips&#x2F;cli-tip-13&#x2F;">CLI tip 13: join lines of two files based on the first field</a>
131+
</span>
132+
</div>
133+
127134
<div class="taxonomy__item">
128135
<span class="taxonomy__item__time">2022-06-29</span>
129136
<span class="taxonomy__item__title">

Diff for: tags/tip/index.html

+7
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-07-20</span>
129+
<span class="taxonomy__item__title">
130+
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tips&#x2F;cli-tip-13&#x2F;">CLI tip 13: join lines of two files based on the first field</a>
131+
</span>
132+
</div>
133+
127134
<div class="taxonomy__item">
128135
<span class="taxonomy__item__time">2022-07-13</span>
129136
<span class="taxonomy__item__title">

Diff for: tips/cli-tip-13/index.html

+211
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
5+
<meta http-equiv="content-type" content="text/html; charset=utf-8">
6+
7+
<!-- Enable responsiveness on mobile devices-->
8+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
9+
10+
<title>CLI tip 13: join lines of two files based on the first field</title>
11+
12+
13+
<link rel="alternate" type="application/atom+xml" title="RSS" href="https://learnbyexample.github.io/atom.xml">
14+
15+
16+
17+
<script src="https://cdnjs.cloudflare.com/ajax/libs/slideout/1.0.1/slideout.min.js"></script>
18+
19+
20+
21+
22+
<link rel="stylesheet" href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;site.css">
23+
24+
25+
26+
27+
28+
<link rel="icon" href="https://learnbyexample.github.io/favicon.svg">
29+
<link rel="shortcut icon" href="https://learnbyexample.github.io/favicon.png">
30+
</head>
31+
32+
<body>
33+
<div class="container">
34+
35+
<div id="mobile-navbar" class="mobile-navbar">
36+
<div class="mobile-header-logo">
37+
<a href="/" class="logo">learnbyexample</a>
38+
</div>
39+
<div class="mobile-navbar-icon icon-out">
40+
<span></span>
41+
<span></span>
42+
<span></span>
43+
</div>
44+
</div>
45+
46+
<nav id="mobile-menu" class="mobile-menu slideout-menu slideout-menu-left">
47+
<ul class="mobile-menu-list">
48+
49+
<li class="mobile-menu-item">
50+
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;books">
51+
Books
52+
</a>
53+
</li>
54+
55+
<li class="mobile-menu-item">
56+
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;mini">
57+
Mini
58+
</a>
59+
</li>
60+
61+
<li class="mobile-menu-item">
62+
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tips">
63+
Tips
64+
</a>
65+
</li>
66+
67+
<li class="mobile-menu-item">
68+
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tags">
69+
Tags
70+
</a>
71+
</li>
72+
73+
<li class="mobile-menu-item">
74+
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;about">
75+
About
76+
</a>
77+
</li>
78+
79+
</ul>
80+
</nav>
81+
82+
<header id="header">
83+
<div class="logo"><a href="https:&#x2F;&#x2F;learnbyexample.github.io">learnbyexample</a></div>
84+
<nav class="menu">
85+
<ul>
86+
87+
<li>
88+
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;books">
89+
Books
90+
</a>
91+
</li>
92+
93+
<li>
94+
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;mini">
95+
Mini
96+
</a>
97+
</li>
98+
99+
<li>
100+
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tips">
101+
Tips
102+
</a>
103+
</li>
104+
105+
<li>
106+
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tags">
107+
Tags
108+
</a>
109+
</li>
110+
111+
<li>
112+
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;about">
113+
About
114+
</a>
115+
</li>
116+
117+
</ul>
118+
</nav>
119+
</header>
120+
121+
<main>
122+
<div class="content" id="mobile-panel">
123+
124+
125+
126+
127+
<article class="post">
128+
129+
<header class="post__header">
130+
<h1 class="post__title">
131+
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tips&#x2F;cli-tip-13&#x2F;">CLI tip 13: join lines of two files based on the first field</a>
132+
</h1>
133+
<div class="post__meta">
134+
<span class="post__time">2022-07-20</span>
135+
136+
</div>
137+
</header>
138+
139+
<div class="post-content">
140+
<p>By default, <code>join</code> combines two files based on the first field content (also referred as <strong>key</strong>). Only the lines with common keys will be part of the output. The key field will be displayed first in the output (this distinction will come into play if the first field isn't the key). Rest of the line will have the remaining fields from the first and second files, in that order. One or more blanks (space or tab) will be considered as the input field separator and a single space will be used as the output field separator. If present, blank characters at the start of the input lines will be ignored.</p>
141+
<pre style="background-color:#f5f5f5;">
142+
<code><span style="color:#7f8989;"># sample sorted input files
143+
</span><span style="color:#1f1f1f;">$ cat jan.txt
144+
apple </span><span style="color:#b3933a;">10</span><span style="color:#1f1f1f;">
145+
banana </span><span style="color:#b3933a;">20</span><span style="color:#1f1f1f;">
146+
soap </span><span style="color:#b3933a;">3</span><span style="color:#1f1f1f;">
147+
tshirt </span><span style="color:#b3933a;">3</span><span style="color:#1f1f1f;">
148+
$ cat feb.txt
149+
banana </span><span style="color:#b3933a;">15</span><span style="color:#1f1f1f;">
150+
fig </span><span style="color:#b3933a;">100</span><span style="color:#1f1f1f;">
151+
pen </span><span style="color:#b3933a;">2</span><span style="color:#1f1f1f;">
152+
soap </span><span style="color:#b3933a;">1
153+
154+
</span><span style="color:#7f8989;"># combine common lines based on the first field
155+
</span><span style="color:#1f1f1f;">$ join jan.txt feb.txt
156+
banana </span><span style="color:#b3933a;">20 15</span><span style="color:#1f1f1f;">
157+
soap </span><span style="color:#b3933a;">3 1
158+
</span></code></pre>
159+
<p>Here's an <code>awk</code> version to do the same. Helpful if you want to do some additional processing that won't be possible with the <code>join</code> command. Another advantage is that this solution will work even if the input files are not sorted.</p>
160+
<pre style="background-color:#f5f5f5;">
161+
<code><span style="color:#1f1f1f;">$ awk </span><span style="color:#d07711;">&#39;NR==FNR{a[$1]=$2; next} $1 in a{print $1, a[$1], $2}&#39;</span><span style="color:#1f1f1f;"> jan.txt feb.txt
162+
banana </span><span style="color:#b3933a;">20 15</span><span style="color:#1f1f1f;">
163+
soap </span><span style="color:#b3933a;">3 1
164+
</span></code></pre>
165+
<p><strong>Video demo</strong>:</p>
166+
<p align="center"><iframe width="560" height="315" loading="lazy" src="https://www.youtube.com/embed/Pjku4w7J6Zg" title="YouTube video player" frameborder="0" allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p>
167+
<br>
168+
<p><img src="/images/info.svg" alt="info" /> See <a href="https://learnbyexample.github.io/cli_text_processing_coreutils/join.html">join chapter</a> 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>
169+
170+
</div>
171+
172+
173+
174+
175+
<div class="post-footer">
176+
177+
178+
<div class="post-tags">
179+
180+
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tags&#x2F;linux&#x2F;">#linux</a>
181+
182+
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tags&#x2F;command-line&#x2F;">#command-line</a>
183+
184+
<a href="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;tags&#x2F;tip&#x2F;">#tip</a>
185+
186+
</div>
187+
188+
189+
190+
191+
192+
</div>
193+
194+
195+
196+
</article>
197+
198+
199+
</div>
200+
</main>
201+
202+
203+
204+
</div>
205+
206+
207+
<script type="text/javascript" src="https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;even.js" ></script>
208+
209+
</body>
210+
211+
</html>

Diff for: tips/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ <h2 id="python-snake"><p style="color: #ff6600"><a name="python"></a>Python 🐍
143143
<hr>
144144
<h2 id="command-line-tools-penguin"><p style="color: #ff6600"><a name="command-line-tools"></a>Command line tools 🐧</h2>
145145
<ul>
146+
<li><a href="https://learnbyexample.github.io/tips/cli-tip-13/">CLI tip 13: join lines of two files based on the first field</a></li>
146147
<li><a href="https://learnbyexample.github.io/tips/cli-tip-12/">CLI tip 12: squeeze empty lines</a></li>
147148
<li><a href="https://learnbyexample.github.io/tips/cli-tip-11/">CLI tip 11: longest line length</a></li>
148149
<li><a href="https://learnbyexample.github.io/tips/cli-tip-10/">CLI tip 10: version sort</a></li>

0 commit comments

Comments
 (0)