Tutorial for a Unix Join
The Unix join command takes two text files as input and joins lines that start with a common text field together, dropping other lines in the text files where they don't share the text field at the beginning of the line in common. For example, if you have two files, one with "abc 123" and "abc 456," and perform a join on them, the command prints "abc 123 456" into the standard terminal output. However, if you have two files, one with "123 abc" and "456 abc," the join command prints nothing, since neither line starts with the same text. You can configure join to match other text fields with command line switches.
Instructions
-
-
1
Open a terminal by clicking "Applications," "Accessories" and "Terminal."
-
2
Use the "cd" command to enter the directory containing the text files you want to run the join command on. For example, type "cd Documents" into the terminal and press "Enter" to enter the Documents directory.
-
-
3
Type "join file1 file2," replacing "file1" and "file2" with the names of the files, into the terminal and press "Enter." The join command combines each line that begins with the same text and prints the combined line in the terminal.
-
1
Tips & Warnings
You also can add "-a1" and/or "-a2" to the join command by typing "join -a1 -a2 file1 file2." Join also prints all the lines that can't be paired from file1 (-a1) and file2 (-a2.)
Type "join -1 2 -2 2 file1 file2" to have the join command use the second column of both files to attempt to match. If you type this, the lines "123 abc" and "456 abc" will match, because join looks for and matches on the second text field, "abc."
Type "man join" and press "Enter" to see all the join command's command line switches.