Linux join Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux join command to merge two or more files based on a common field. The join command is a powerful tool for text processing and editing, allowing you to combine data from multiple sources into a single output. You will start by understanding the purpose and syntax of the join command, then practice joining two files based on common fields, and finally learn how to join multiple files using this command. The lab covers practical examples and provides a comprehensive understanding of the join command's functionality, making it a valuable skill for data analysis and file management tasks in a Linux environment.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/cut("`Text Cutting`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/TextProcessingGroup -.-> linux/join("`File Joining`") subgraph Lab Skills linux/cat -.-> lab-422751{{"`Linux join Command with Practical Examples`"}} linux/cut -.-> lab-422751{{"`Linux join Command with Practical Examples`"}} linux/echo -.-> lab-422751{{"`Linux join Command with Practical Examples`"}} linux/join -.-> lab-422751{{"`Linux join Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the join Command

In this step, you will learn about the purpose and syntax of the join command in Linux. The join command is used to merge two or more files based on a common field.

The basic syntax of the join command is:

join [options] file1 file2

The most common options are:

  • -t <char>: Specify a delimiter character to use instead of the default whitespace.
  • -i or -I: Ignore case when comparing fields.
  • -1 <field>: Join on the specified field from the first file.
  • -2 <field>: Join on the specified field from the second file.

Let's start by creating two sample files to demonstrate the join command:

$ cat file1.txt
1001 John
1002 Jane
1003 Bob
1004 Alice
$ cat file2.txt
1001 Sales
1002 Marketing
1003 IT
1004 HR

Example output:

1001 John Sales
1002 Jane Marketing
1003 Bob IT
1004 Alice HR

In the above example, the join command merges the two files based on the first field (the employee ID), creating a new line for each matching pair of records.

Join Two Files Based on Common Fields

In this step, you will learn how to join two files based on a common field using the join command.

Let's create two more sample files to demonstrate the process:

$ cat departments.txt
1001 Sales
1002 Marketing
1003 IT
1004 HR
$ cat employees.txt
1001 John
1002 Jane
1003 Bob
1004 Alice

To join the departments.txt and employees.txt files based on the first field (the employee ID), we can use the following command:

$ join -t ' ' -1 1 -2 1 departments.txt employees.txt
1001 Sales John
1002 Marketing Jane
1003 IT Bob
1004 HR Alice

The options used in this command are:

  • -t ' ': Use a space character as the delimiter.
  • -1 1: Join on the first field (employee ID) in the first file (departments.txt).
  • -2 1: Join on the first field (employee ID) in the second file (employees.txt).

The output shows the merged records, with the corresponding department and employee name for each employee ID.

Join Multiple Files with the join Command

In this final step, you will learn how to join multiple files using the join command.

Let's create one more sample file to join with the previous files:

$ cat locations.txt
1001 New York
1002 Los Angeles
1003 Chicago
1004 Miami

To join the departments.txt, employees.txt, and locations.txt files based on the first field (the employee ID), we can use the following command:

$ join -t ' ' -1 1 -2 1 departments.txt \
       | join -t ' ' -1 1 -2 1 - employees.txt \
       | join -t ' ' -1 1 -2 1 - locations.txt
1001 Sales John New York
1002 Marketing Jane Los Angeles
1003 IT Bob Chicago
1004 HR Alice Miami

In this command, we use the join command three times, chaining the output of the first two joins as the input for the third join. This allows us to merge all three files based on the common employee ID field.

The options used in this command are the same as the previous step:

  • -t ' ': Use a space character as the delimiter.
  • -1 1: Join on the first field (employee ID) in the first file.
  • -2 1: Join on the first field (employee ID) in the second file.

The final output shows the merged records, with the corresponding department, employee name, and location for each employee ID.

Summary

In this lab, you learned about the purpose and syntax of the join command in Linux, which is used to merge two or more files based on a common field. You started by creating sample files and understanding the basic syntax of the join command, including the use of options like -t, -i, -1, and -2 to specify the delimiter, ignore case, and select the fields to join on. You then practiced joining two files based on a common field, demonstrating how the join command can merge records from different files into a single output.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like