/ factorpad.com / tech / linux-essentials / join-command.html
An ad-free and cookie-free website.
Beginner
Learn to join lines from two files at the Linux command line.
Videos can also be accessed from the Linux Essentials Playlist on YouTube.
Linux paste Command Summary with Examples (4:09)
Our thirty-eighth word, or command to memorize is
join
from our category
Text.
join
allows you to join lines from
two files.
-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.
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 ;).
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.
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
.
Okay good, and last, let's get a drumroll here and
cat
those results.
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
;).
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.
For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:
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.
paste
command,
hit Back.grep
command, click Next./ factorpad.com / tech / linux-essentials / join-command.html
A newly-updated free resource. Connect and refer a friend today.