By default, join combines two files based on the first field content (also referred as key). Only the lines with common keys will be part of the output. The key field will be displayed first in the output (this distinction will come into play if the first field isn't the key). Rest of the line will have the remaining fields from the first and second files, in that order. One or more blanks (space or tab) will be considered as the input field separator and a single space will be used as the output field separator. If present, blank characters at the start of the input lines will be ignored.

# sample sorted input files
$ cat jan.txt
apple   10
banana  20
soap    3
tshirt  3
$ cat feb.txt
banana  15
fig     100
pen     2
soap    1

# combine common lines based on the first field
$ join jan.txt feb.txt
banana 20 15
soap 3 1

Here's an awk version to do the same. Helpful if you want to do some additional processing that won't be possible with the join command. Another advantage is that this solution will work even if the input files are not sorted.

$ awk 'NR==FNR{a[$1]=$2; next} $1 in a{print $1, a[$1], $2}' jan.txt feb.txt
banana 20 15
soap 3 1

Video demo:


info See join chapter from my Command line text processing with GNU Coreutils ebook for more details and examples.