Shell Join command Explanation (GO)

Source: Internet
Author: User

Transfer from http://www.cnblogs.com/mfryf/p/3402200.html

Join command
Function: "Connect the same rows as specified in the two files", that is, the corresponding rows are stitched together in a row according to a column in the two file.
Join [options] file1 file2
Note: These two files must be sorted according to the same rules already on this column.
Join options
-A FileNum: In addition to displaying matching lines, the matching lines in the specified ordinal (1 or 2) file are displayed.
-E EMPTY: The fields that do not exist in the file will be displayed instead of the characters specified by this option
-I: Ignore uppercase and lowercase
-j field: equivalent to -1 FIELD-2 field,-j Specify a field as the matching field
-o format: output in specified format
-T CHAR: delimiter with specified characters as input and output
Join is delimited by default with whitespace characters (spaces and \ t) and can use Join-t $ ' t ' to specify tab delimiter
-V FileNum: similar to-a but with a value indicating that there are no matching lines in the file
-1 field: Match by field in File1
-2 field: Match by field in File2
--help: Print command Help file
Examples:
File File1.txt
AA 1 2
BB 2 3
CC 4 6
DD 3 3
File File2.txt
AA 2 1
BB 8 2
FF 2 4
CC 4 4
DD 5 5

1.join file1.txt File2.txt
Output: AA 1 2 2 1
BB 2 3 8 2
By default, the first line of two files is the matching field, and the default is delimited by a space (unlimited number).
2.join-j 1 File1.txt file2.txt
Output: AA 1 2 2 1
BB 2 3 8 9
The-J option specifies that matching fields in the first column of two files are equivalent to join File1.txt file2.txt
3. join-1 2-2 3 file1.txt file2.txt
Output: 1 AA 2 AA 2
2 BB 3 BB 8
4 CC 6 FF 2
4 CC 6 CC 4
Match fields with the second column of the first file and the third column of the second file. Because the second file in the third column of two 3 is the first file in the third row so output
4 CC 6 FF 2
4 CC 6 CC 4
4 join-o 1.1-o 1.2-o 1.3-o 2.1-o 2.2-o 2.3-e ' empty '-a 1 file1.txt file2.txt
Output: AA 1 2 AA 2 1
BB 2 3 BB 8 2
CC 4 6 Empty empty empty
DD 3 3 Empty empty empty
-o Specifies that the file1 columns of the file2 are output. -a specifies that rows that do not match in the file1 are also output, but File2 does not have the corresponding fields in the two lines after file1, so use empty to complement them.

5.join-v 1 File1.txt file2.txt
Output: CC 4 6
DD 3 3
-V 1 outputs unmatched rows in file1
The Ps:join command is similar to the join command in the database.
Although both file1 and file2 are sorted, the first two rows are matched only by a mismatch in the third row, and subsequent rows, although the fields can be matched, cannot match. The join command, which requires a strong file format, assumes that you want to use it more flexibly, using awk commands to participate in an awk instance

6. Join standard input
Sometimes we need to join multiple formats of the same file, and join accepts instructions for two files, so we can use the pipe and the character "-" to implement the
join File1 file2 | join-file3 | joi N-file4
This will allow you to connect four files together.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.