FactorPad
Build a Better Process

Linux join Command Summary with Examples

Linux join allows you to join lines from two files.
  1. Purpose - Learn what join is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with join.
  4. A tip - Finish off with one more insight.
face pic by Paul Alan Davis, CFA
Updated: February 23, 2021
In this tutorial on Linux join, 38 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / join-command.html


An ad-free and cookie-free website.


Examples of the Linux join Command

Beginner

Learn to join lines from two files at the Linux command line.

Video Tutorial

Videos can also be accessed from the Linux Essentials Playlist on YouTube.

Linux paste Command Summary with Examples (4:09)

Video Script

The Command and Why You Need It

Our thirty-eighth word, or command to memorize is join from our category Text.

join allows you to join lines from two files.

Common Linux join Options
-options description
--header Treat first line as a header
-1 FIELD Join on FIELD in the first file
-2 FIELD Join on FIELD in the second file

Recall from the previous five videos, we made a delimited file, like one you might analyze in a spreadsheet. Now we'll join it with another file.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The join command has 13 options, and the arguments are the two files you'd like to join.

Like most commands, help is available with double-dash --help. And the --header option is for files with headers. Here we'll be connecting two files on a field common using these two options here ( -1 FIELD and -2 FIELD).

So why is join an important command? Well, if you have structured tabular data, like you might in the scientific fields, then you'll need to connect files. And now you know how to do that.

Demonstration

Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go. Let's first take a look at the two files we're connecting, the first created in the last video, video38a.txt, shows dates, video text files and number of views one year from now, found using a crystal ball ;).

$ cat -n video38a.txt Oct 4:video05.txt:6 Oct 10:video11.txt:8 Oct 10:video16.txt:819302 Oct 12:video19.txt:60 Oct 12:video20.txt:669821 Oct 13:video21.txt:981032 Oct 13:video23.txt:69 Oct 15:video24.txt:781420 Oct 15:video26.txt:9 Oct 15:video27.txt:897502 Oct 17:video31.txt:8 Oct 18:video32.txt:86 Oct 18:video33.txt:789123 $ clear

Some are popular, and some are not.

Next, I've also created another text file, video38b.txt listing the content creator for each of the 13 videos. Myself or Jennifer.

$ cat -n video38b.txt 1 video05.txt:Paul 2 video11.txt:Paul 3 video16.txt:Jennifer 4 video19.txt:Paul 5 video20.txt:Jennifer 6 video21.txt:Jennifer 7 video23.txt:Paul 8 video24.txt:Jennifer 9 video26.txt:Paul 10 video27.txt:Jennifer 11 video31.txt:Paul 12 video32.txt:Paul 13 video33.txt:Jennifer $ clear

Next, let's see who's better, join in table 1 field 2, and table 2 on field 1, -t ":", where we put in the delimiter here with a colon, then the files, and what we'll do is write that to video38c.txt.

$ join -1 2 -2 1 -t ":" video38a.txt video38b.txt > video38c.txt

Okay good, and last, let's get a drumroll here and cat those results.

$ cat video38c.txt video05.txt:Oct 4:6:Paul video11.txt:Oct 10:8:Paul video16.txt:Oct 10:819302:Jennifer video19.txt:Oct 12:60:Paul video20.txt:Oct 12:669821:Jennifer video21.txt:Oct 13:981032:Jennifer video23.txt:Oct 13:69:Paul video24.txt:Oct 14:781420:Paul video26.txt:Oct 15:9:Paul video27.txt:Oct 15:897502:Jennifer video31.txt:Oct 17:8:Paul video32.txt:Oct 18:86:Paul video33.txt:Oct 18:789123:Jennifer

Look at this. Video (row) 1, 6 views for Paul. Video (row) 2, 8 views for Paul. Video (row) 3, 819 thousand views for Jennifer. Hmmm. Oh wow. ouch ;).

A Final Tip

Okay now you know how to use join. And you know the syntax for commands, options and arguments.

One last tip about the join command. So join has a handy option, -e that will input a default, like NA, when data is missing.

Okay, thanks for visiting today. I hope this was a helpful introduction to the join command.


Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:


What's Next?

If you haven't been able to tell, here we like our comedy dry like the desert. It may come across better on video, subscribe here.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / join-command.html


linux join command
join linux
linux join
unix join
join unix
linux join examples
linux join files
join command
linux combine two files
linux merge files
linux join examples
join command syntax
linux join summary
join command options
linux join tutorial
man join

A newly-updated free resource. Connect and refer a friend today.