JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Linux connection command join

Author:JIYIK Last Updated:2025/04/08 Views:

The join command is also a member of the pipeline command family. Its function is to connect the data of those rows with equal columns (that is, the first column) in each file.

First we prepare two files /tmp/join1 and /tmp/join2.

# cat /tmp/join1
www onmpw
domain jiyi
w3 blog
join command
ls l
# cat /tmp/join2
www com
domain cn
w3 net
Join org
wc l

After preparing the above two files, let's take a look at the usage of join without options.

# join /tmp/join1 /tmp/join2
www onmpw com
domain jiyi cn
w3 blog net

Do you see the result? The data in the first column of the first three rows in the join1 and join2 files are equal, so these three rows will be concatenated. For the other two rows, the strings in the first column are not equal, so they will not be concatenated.

Here we may notice that there is join in the join1 file, and the first column of the line in join2 is Join. How should this be matched? By default, join is case-sensitive, so the two are considered unequal. To make the two equal, we need to use the -i option of join.

-i ignore case

# join /tmp/join1 /tmp/join2
www onmpw com
domain jiyi cn
w3 blog net
join command org

Let’s see if there is one more line.

-a FILENUM Print the unjoined lines of the file FILENUM after the normal results.

# join –a 2 /tmp/join1 /tmp/join2
www onmpw com
domain jiyi cn
w3 blog net
Join org
wc l
# join –a 1 /tmp/join1 /tmp/join2
www onmpw com
domain jiyi cn
w3 blog net
join command
ls l

You can understand the meaning of the -a option by looking at the above results. In /tmp/join1, join command and ls l are not connected, so when -a is specified as 1, they are printed out. Similarly, when -a is specified as 2, Join org and wc l are printed out.

-v FILENUM This option has similar functions to -a, the only difference is that -v will not output connected lines, but only output unconnected lines in the specified file.

# join –v 1 /tmp/join1 /tmp/join2
join command
ls l
# join –v 2 /tmp/join1 /tmp/join2
Join org
wc l

-t specifies the delimiter. By default, the fields we are talking about are separated by spaces. With this option, we can change the fields.

# cat /tmp/join3
domain:cs
www:com
domain:cn
w3:net
Join:org
wc:l
# cat /tmp/join4
www:onmpw
domain:jiyi
w3:blog
join:command
ls:l

Let's first look at the default use of space splitting

# join /tmp/join3 /tmp/join4
//The result is empty because each column is not equal

Next, use -t to specify the delimiter

# join –t : /tmp/join3 /tmp/join4
www:com:onmpw
domain:cn:jiyi
w3:net:blog

Let's see if there is any result. Yes, this is the usage of -t.

-o specifies the column to be joined. What does this mean? By default, the first column in common between the two files is joined, and then the contents of the two files are joined in the order of file1 and file2. Using this option is to specify the column we want to join, rather than joining all the columns.

Its format is as follows

-o MN,MN

Here M indicates the number of files, and N indicates the Nth column of the connection.

-o 1.2,2.2

This example means connecting the second column of file 1 and the second column of file 2, of course, the premise is that the two meet the connection conditions.

# join /tmp/join1 /tmp/join2 //First look at the default situation
www onmpw com
domain jiyi cn
w3 blog net

We can see that the output results are the same columns first, then join1 and then join2.

# join –o 1.1,2.2 /tmp/join1 /tmp/join2
www com
domain cn
w3 net

Look at the results displayed in the connection field we specified.

# join –o 1.2,2.2 /tmp/join1 /tmp/join2
onmpw com
jiyi cn
blog net

Specifies that the second column of the first file is displayed concatenated with the second column of the second file. Now you should understand the usage of -o!

-1 FIELD specifies the first file field to compare

-2 FIELD specifies the field of the second file to be compared

As we know, by default, the first field of the two files is used for comparison. Use the above options to change the field to be compared.

# cat /tmp/join5
onmpw www
jiyi domain
blog w3
command join
l ls

First look at the results of join5 and join2 by default

# join /tmp/join5 /tmp/join2
//The result is empty

The reason for the empty result is that the first field of the join5 file is not equal to the first field of the join2 file, so there are no rows to join.

# join -1 2 /tmp/join5 /tmp/join2
www onmpw com
domain jiyi cn
w3 blog net

We specified to use the second field of the first file, that is, the join5 file, and compared it with the first field of the join2 file, and the result appeared because these two fields have equal strings.

After understanding the above example, it is not difficult to understand the use of -1 FIELD and -2 FIELD together.

# join -1 2 -2 2 /tmp/join5 /tmp/join2
//The result is empty

-j FIELD This option is equivalent to -1 FIELD -2 FIELD. No more examples are given here.

This is the end of the introduction to the specific usage of the join command. Finally, it should be noted that it is best to sort the files to be processed before using join to connect them, otherwise some of the compared data will be skipped.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Joining columns using Select in PostgreSQL

Publish Date:2025/04/11 Views:176 Category:PostgreSQL

MySQL PostgreSQL is an object-relational database system, which means it can support more complex data types than its competitors . Today we will learn how to use SELECT the operator to join the columns of a table. Using operators to || joi

Execute multiple joins in one query in MYSQL

Publish Date:2025/04/11 Views:94 Category:MySQL

Have you ever wondered how to include multiple joins in one query in MySQL? You have come to the right place. Remember that joins allow us to access information from other tables. This information is included separately to avoid redundancy.

Joining 3 tables in MySQL

Publish Date:2025/04/11 Views:187 Category:MySQL

In this tutorial, we will learn how to join three tables in MySQL. Businesses and organizations may have to visualize three tables simultaneously based on certain matching columns common to all three tables. This operation is allowed in MyS

Use of UPDATE JOIN in MySQL

Publish Date:2025/04/11 Views:85 Category:MySQL

This tutorial will explain how to use the statement in MySQL database UPDATE JOIN . We generally use joins to iterate over the rows in a particular table which may or may not have similar rows in other tables. We can UPDATE use JOIN the cla

Left lateral join in PostgreSQL

Publish Date:2025/04/10 Views:50 Category:PostgreSQL

The PostgreSQL official documentation states, ``LATERAL 关键字可以位于子 SELECT FROM 项之前。这允许子 SELECT 引用出现在 FROM 中的 FROM 项的列(如果没有 LATERAL ,每个子 SELECT 都是独立评估的,因此不

Restart PostgreSQL in Ubuntu 18.04

Publish Date:2025/04/09 Views:72 Category:PostgreSQL

This short article shows how to restart PostgreSQL in Ubuntu. Restart PostgreSQL Server in Ubuntu You can restart Postgres server in Ubuntu using the following command. Order: sudo service postgres restart Sometimes the above command does n

Issues to note when installing Apache on Linux

Publish Date:2025/04/08 Views:78 Category:OPERATING SYSTEM

As the most commonly used web server, Apache can be used in most computer operating systems. As a free and open source Unix-like operating system, Linux and Apache are a golden pair. This article will introduce the installation and use of A

How to decompress x.tar.xz format files under Linux

Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM

A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr

Summary of vim common commands

Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM

In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial