{"id":166,"date":"2012-02-23T17:36:28","date_gmt":"2012-02-24T01:36:28","guid":{"rendered":"http:\/\/sobell.com\/mgsblog\/?p=166"},"modified":"2012-02-23T17:36:28","modified_gmt":"2012-02-24T01:36:28","slug":"performing-a-join-from-the-command-line","status":"publish","type":"post","link":"https:\/\/sobell.com\/mgsblog\/archives\/166","title":{"rendered":"Performing a Join from the Command Line"},"content":{"rendered":"<p>The examples in this section use the following files:<\/p>\n<pre><code>$ <strong>cat one<\/strong>\r\n9999 first line file one.\r\naaaa second line file one.\r\ncccc third line file one.\r\n\r\n$ <strong>cat two<\/strong>\r\naaaa FIRST line file two.\r\nbbbb SECOND line file two.\r\ncccc THIRD line file two.<\/code><\/pre>\n<p>The first example shows the simplest use of <strong>join<\/strong>. The files named <strong>one<\/strong> and <strong>two<\/strong> are joined based, by default, on the first field in each line of both files. Both files are in sorted order based on the join field. The join fields on two pairs of lines match and <strong>join<\/strong> displays those lines.<\/p>\n<pre><code>$ join one two\r\naaaa second line file one. FIRST line file two.\r\ncccc third line file one. THIRD line file two.<\/code><\/pre>\n<p>You can use the <strong>\u2013 \u2013check-order<\/strong> option to see if both files are properly sorted. Following, <strong>sort<\/strong> with the <strong>\u2013r<\/strong> option sorts one in reverse alphabetical order and sends the output through a pipe to <strong>join<\/strong>. The shell replaces the <strong>\u2013<\/strong> argument to <strong>join<\/strong> with the standard input to <strong>join<\/strong> which comes from the pipe; <strong>join<\/strong> displays an error message.<\/p>\n<pre><code>$ sort -r one | join --check-order - two\r\njoin: file 1 is not in sorted order<\/code><\/pre>\n<p>Next, the <strong>\u2013a<\/strong> option with an argument of <strong>1<\/strong> causes <strong>join<\/strong> to display, in addition to its normal output, lines from the first file (<strong>one<\/strong>) that do not have a matching join field.<\/p>\n<pre><code>$ join -a 1 one two\r\n9999 first line file one.\r\naaaa second line file one. FIRST line file two.\r\ncccc third line file one. THIRD line file two.<\/code><\/pre>\n<p>Use <strong>\u2013v<\/strong> in place of <strong>\u2013a<\/strong> to inhibit the display of lines that <strong>join<\/strong> normally displays (those that have a matching join field).<\/p>\n<pre><code>$ join -v 1 one two\r\n9999 first line file one.<\/code><\/pre>\n<p>The final example uses <strong>onea<\/strong> as the first file and specifies the third field of the first file (<strong>\u20131 3<\/strong>) as the match field. The second file (<strong>two<\/strong>) uses the default (first) field for matching.<\/p>\n<pre><code>$ cat onea\r\nfirst line aaaa file one.\r\nsecond line 1111 file one.\r\nthird line cccc file one.\r\n$ join -1 3 onea two\r\naaaa first line file one. FIRST line file two.\r\ncccc third line file one. THIRD line file two.<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The examples in this section use the following files: $ cat one 9999 first line file one. aaaa second line file one. cccc third line file one. $ cat two aaaa FIRST line file two. bbbb SECOND line file two. &hellip; <a href=\"https:\/\/sobell.com\/mgsblog\/archives\/166\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,3],"tags":[],"_links":{"self":[{"href":"https:\/\/sobell.com\/mgsblog\/wp-json\/wp\/v2\/posts\/166"}],"collection":[{"href":"https:\/\/sobell.com\/mgsblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sobell.com\/mgsblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sobell.com\/mgsblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sobell.com\/mgsblog\/wp-json\/wp\/v2\/comments?post=166"}],"version-history":[{"count":14,"href":"https:\/\/sobell.com\/mgsblog\/wp-json\/wp\/v2\/posts\/166\/revisions"}],"predecessor-version":[{"id":181,"href":"https:\/\/sobell.com\/mgsblog\/wp-json\/wp\/v2\/posts\/166\/revisions\/181"}],"wp:attachment":[{"href":"https:\/\/sobell.com\/mgsblog\/wp-json\/wp\/v2\/media?parent=166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sobell.com\/mgsblog\/wp-json\/wp\/v2\/categories?post=166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sobell.com\/mgsblog\/wp-json\/wp\/v2\/tags?post=166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}