join: Join lines of two files on a common field
August 12th, 2024 12:43 PM Mr. Q Categories: Command
The join
command is used to merge lines of two files based on a common field. It is commonly used for combining data from two files with a shared key or column.
Command:
Copy001join [options] file1 file2
Examples
- Join Two Files on the First Field
Copy001 join file1.txt file2.txt
Sample File (file1.txt
):
Copy001 1 Alice
002 2 Bob
003 3 Charlie
Sample File (file2.txt
):
Copy001 1 Engineer
002 2 Designer
003 3 Manager
Sample Output:
Copy001 1 Alice Engineer
002 2 Bob Designer
003 3 Charlie Manager
- Join on a Specific Field and Use a Custom Delimiter
Copy001 join -t ',' -1 2 -2 1 file1.csv file2.csv
Sample File (file1.csv
):
Copy001 Name,1
002 Bob,2
003 Charlie,3
Sample File (file2.csv
):
Copy001 1,Engineer
002 2,Designer
003 3,Manager
Sample Output:
Copy001 Bob,2,Designer
002 Charlie,3,Manager
- Print Unmatched Lines from Each File
Copy001 join -v 1 file1.txt file2.txt
Sample File (file1.txt
):
Copy001 1 Alice
002 2 Bob
003 4 Dave
Sample File (file2.txt
):
Copy001 1 Engineer
002 2 Designer
003 3 Manager
Sample Output:
Copy001 4 Dave
- Join Files Using a Different Field
Copy001 join -1 2 -2 1 file1.txt file2.txt
Sample File (file1.txt
):
Copy001 Alice 1
002 Bob 2
003 Charlie 3
Sample File (file2.txt
):
Copy001 Engineer 1
002 Designer 2
003 Manager 3
Sample Output:
Copy001 Alice 1 Engineer
002 Bob 2 Designer
003 Charlie 3 Manager
The join
command is effective for combining data based on a shared key or column, making it useful for various data processing tasks.