The join command

Consider two files, namely one.txt and two.txt:

  • The content of one.txt is as follows:
1 India 
2 UK 
3 Canada 
4 US 
5 Ireland 
  • The content of two.txt is as follows:
1 New Delhi 
2 London 
3 Toronto 
4 Washington 
5 Dublin 

In this case, for both the files, the common fields are the fields that have serial numbers that are the same in both files. We can combine both files using the following command:

    $ join one.txt two.txt

The output will be as follows:

    1 India New Delhi
    2 UK London
    3 Canada Toronto
    4 US Washington
    5 Ireland Dublin

Get Learning Linux Shell Scripting - Second Edition 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.