Merge sorted files (POSIX)
join [-1 n] [-2 n] [-a file_number] [-e string] [-j file_number n] [-o list] [-t char] [-v file_number] file1 file2
Options -a, -j, and -v use the file_number argument. Specify 1 to refer to file1, or 2 to refer to file2.
Use options -1 and -2 in place of option -j.
where field is a field number. You can use a space or a comma to separate entries.
Output is written only for lines with matching join fields. The join field isn't written unless it's included in list.
The join utility forms a “join” of the two relations specified by the lines of file1 and file2. The join is written to the standard output.
The files file1 and file2 are compared on the basis of a “join field” found in both files. For every pair of lines in file1 and file2 that have identical join fields, join prints one output line. The output line normally consists of the join field, followed by the rest of the line from file1 and then the rest of the line from file2. By default, the join field is the first field in each line.
Both file1 and file2 should be sorted in an increasing collating sequence on their join fields (i.e. the same sequence performed by sort -b). Otherwise, some field matches may not be reported. Note, however, that when option -t is specified, the collating sequence should be the same as that produced by sort, without the -b option.
The default input field separators are blanks. In this case, multiple separators count as one field separator; leading separators are ignored. The default output field separator is a space.
Join the password file and group file, matching on the numeric group ID and outputting the login name, group name, and login directory. It's assumed that the files have been sorted in collating sequence on the group ID fields.
join -1 4 -2 3 -o 1.1 2.1 1.6 -t: /etc/passwd /etc/group
gawk, comm, sort, uniq