Joining Files by Field with join
The join command combines two files by matching the contents of specified fields within the files. Fields are typically space-separated entries on a line, although you can specify another character as the field separator with the -t char option, where char is the character you want to use. You can cause join to ignore case when performing comparisons by using the -i option.
The effect of join may best be understood through a demonstration. Consider Listings 1.1 and 1.2, which contain data on telephone numbers; Listing 1.1 shows the names associated with those numbers, and Listing 1.2 shows whether the numbers are listed or unlisted.
Listing 1.1: Demonstration File Containing Telephone Numbers and Names
555-2397 Beckett, Barry
555-5116 Carter, Gertrude
555-7929 Jones, Theresa
555-9871 Orwell, Samuel
Listing 1.2: Demonstration File Containing Telephone Number Listing Status
555-2397 unlisted
555-5116 listed
555-7929 listed
555-9871 unlisted
You can display the contents of both files using join:
$ join listing1.1.txt listing1.2.txt 555-2397 Beckett, Barry unlisted 555-5116 Carter, Gertrude listed 555-7929 Jones, Theresa listed 555-9871 Orwell, Samuel unlisted