Hack 20. Join Command Examples

by Ramesh

Join command combines lines from two files based on a common field.

In the example below, we have two files – employee.txt and salary.txt. Both have employee-id as common field. So, we can use join command to combine the data from these two files using employee-id as shown below.

  1. $ cat employee.txt
  2.  
  3. 100 Jason Smith
  4. 200 John Doe
  5. 300 Sanjay Gupta
  6. 400 Ashok Sharma
  7.  
  8. $ cat bonus.txt
  9.  
  10. 100 $5,000
  11. 200 $500
  12. 300 $3,000
  13. 400 $1,250
  14.  
  15. $ join employee.txt bonus.txt
  16.  
  17. 100 Jason Smith $5,000
  18. 200 John Doe $500
  19. 300 Sanjay Gupta $3,000
  20. 400 Ashok Sharma $1,250