FactorPad
Faster Learning Tutorials

Linux join command summary with examples

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.
Paul Alan Davis, CFA, October 21, 2016
Updated: August 1, 2018
In this tutorial, 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 Next

~/ home  / tech  / linux essentials  / join command


The Linux join command

Beginner

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

Video Tutorial

Linux paste command summary with examples (4:09)

Videos can also be accessed from the YouTube Playlist.

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.

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, we like our comedy dry like the desert. It may come across better on video, subscribe here.

  • For the Outline to all 100 tutorials, click Outline.
  • To go back to the paste command, hit Back.
  • To learn how to search multiple files using the important grep command, click Next.

Outline Back Next

~/ home  / tech  / linux essentials  / join command



 
 
Keywords:
linux join command
linux join text files
join linux
unix join
linux combine two files
linux merge files
linux connect files
linux join examples
linux join files
linux modify columnar data
linux database join
linux merge files
join command syntax
linux join summary
join command options
linux join tutorial
man join
linux join two files