4. join
The command join
, like paste
, adds columns to a file. However it
does it in a way that is similar to the join operation in
relational databases. It joins data from multiple files based on a
shared key field.
-
To demonstrate
join
let's make a couple of files with a shared key. The first file will contain the release dates and the release names:cut -f 1,1 distros-by-date.txt > distros-names.txt
paste \
distros-dates.txt \
distros-names.txt \
> distros-key-names.txthead distros-key-names.txt
-
The second file will contain the release dates and the version numbers:
cut -f 2,2 distros-by-date.txt > distros-vernums.txt
paste \
distros-dates.txt \
distros-vernums.txt \
> distros-key-vernums.txthead distros-key-vernums.txt
-
Both of these files have the release date as a common field. Let's join them:
join \
distros-key-names.txt \
distros-key-vernums.txt \
| headIt is important that the files must be sorted on the key field for
join
to work properly.
Download lesson07/part4.cast
Loading asciinema cast...