|
| 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://learnbyexample.github.io/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://learnbyexample.github.io/books"> |
| 51 | + Books |
| 52 | + </a> |
| 53 | + </li> |
| 54 | + |
| 55 | + <li class="mobile-menu-item"> |
| 56 | + <a href="https://learnbyexample.github.io/mini"> |
| 57 | + Mini |
| 58 | + </a> |
| 59 | + </li> |
| 60 | + |
| 61 | + <li class="mobile-menu-item"> |
| 62 | + <a href="https://learnbyexample.github.io/tips"> |
| 63 | + Tips |
| 64 | + </a> |
| 65 | + </li> |
| 66 | + |
| 67 | + <li class="mobile-menu-item"> |
| 68 | + <a href="https://learnbyexample.github.io/tags"> |
| 69 | + Tags |
| 70 | + </a> |
| 71 | + </li> |
| 72 | + |
| 73 | + <li class="mobile-menu-item"> |
| 74 | + <a href="https://learnbyexample.github.io/about"> |
| 75 | + About |
| 76 | + </a> |
| 77 | + </li> |
| 78 | + |
| 79 | + </ul> |
| 80 | + </nav> |
| 81 | + |
| 82 | + <header id="header"> |
| 83 | + <div class="logo"><a href="https://learnbyexample.github.io">learnbyexample</a></div> |
| 84 | + <nav class="menu"> |
| 85 | + <ul> |
| 86 | + |
| 87 | + <li> |
| 88 | + <a href="https://learnbyexample.github.io/books"> |
| 89 | + Books |
| 90 | + </a> |
| 91 | + </li> |
| 92 | + |
| 93 | + <li> |
| 94 | + <a href="https://learnbyexample.github.io/mini"> |
| 95 | + Mini |
| 96 | + </a> |
| 97 | + </li> |
| 98 | + |
| 99 | + <li> |
| 100 | + <a href="https://learnbyexample.github.io/tips"> |
| 101 | + Tips |
| 102 | + </a> |
| 103 | + </li> |
| 104 | + |
| 105 | + <li> |
| 106 | + <a href="https://learnbyexample.github.io/tags"> |
| 107 | + Tags |
| 108 | + </a> |
| 109 | + </li> |
| 110 | + |
| 111 | + <li> |
| 112 | + <a href="https://learnbyexample.github.io/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://learnbyexample.github.io/tips/cli-tip-13/">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;">'NR==FNR{a[$1]=$2; next} $1 in a{print $1, a[$1], $2}'</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://learnbyexample.github.io/tags/linux/">#linux</a> |
| 181 | + |
| 182 | + <a href="https://learnbyexample.github.io/tags/command-line/">#command-line</a> |
| 183 | + |
| 184 | + <a href="https://learnbyexample.github.io/tags/tip/">#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://learnbyexample.github.io/even.js" ></script> |
| 208 | + |
| 209 | + </body> |
| 210 | + |
| 211 | +</html> |
0 commit comments