join

join is a utility that will combine two different files based on common keys in both files. Both files have to be sorted on the key for this to work, but that can usually be arranged, even if it means creating a temporary file from the original input. Another limitation is that there has to be a common delimiter used for both input files, and for the output. If you can live with those restrictions, join can be a very useful tool. Using the same input data as the paste section of the next chapter, join can be used to combine different databases. The code that follows shows two files, hosts and ethers. First they are sorted on key 2 (the hostname) so that they are in the same order as each other. The join command then combines these files by hostname.

cat hosts
127.0.0.1       localhost
192.168.1.5     plug
192.168.1.10    declan
192.168.1.11    atomic
192.168.1.13    goldie
192.168.1.227   elvis
$ cat ethers
0a:00:27:00:00:00 plug
01:00:3a:10:21:fe declan
71:1f:04:e3:1b:13 atomic
01:01:8d:07:3a:ea goldie
01:01:31:09:2a:f2 elvis
$ sort -k2 ethers > ethers.sortedsort -k2 hosts > hosts.sortedjoin -j2 -a1 hosts.sorted ethers.sorted
atomic 192.168.1.11 71:1f:04:e3:1b:13
declan 192.168.1.10 01:00:3a:10:21:fe
elvis 192.168.1.227 01:01:31:09:2a:f2
goldie 192.168.1.13  01:01:8d:07:3a:ea
localhost 127.0.0.1
plug 192.168.1.5 0a:00:27:00:00:00
$

Here the common field is the second field in both files, as specified by -j2. The -a1 flag tells join to display everything from the ...

Get Shell Scripting: Expert Recipes for Linux, Bash, and More now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.