Paste - Join 2 files in 2 columns

By xngo on June 29, 2019

Paste command is one of the most overlooked commands in Linux. It is used to join content of files together. For example, let's consider that we have 2 files: fruits.txt and prices.txt. Their content look like this.

~>cat fruits.txt 
banana
pineapple
guava
 
~>cat prices.txt 
1.99
3.45
8.01

Now, you want to join the list of fruits with their corresponding prices. Here is how you do it with paste.

~>paste fruits.txt prices.txt 
banana  1.99
pineapple   3.45
guava   8.01

Join content on the fly

Beside input files, paste can also accept standard input. With this, you can join content from commands on the fly. For example, cal command show the calendar of a specific month. Now, I want to display the calendar of the current and next months side by side. Here is the command.

~>paste <(cal 06 2019) <(cal 07 2019)
     June 2019              July 2019        
Su Mo Tu We Th Fr Sa    Su Mo Tu We Th Fr Sa  
                   1       1  2  3  4  5  6  
 2  3  4  5  6  7  8      7  8  9 10 11 12 13  
 9 10 11 12 13 14 15     14 15 16 17 18 19 20  
16 17 18 19 20 21 22     21 22 23 24 25 26 27  
23 24 25 26 27 28 29     28 29 30 31           
30                                           

Instead of hardcoding the year and month, you can also rewrite the command above to use date to dynamically show the calendar of the current and next months.

~>paste <(cal $(date +'%m %Y')) <(cal $(date -d '+1 month' +'%m %Y'))
     June 2019              July 2019        
Su Mo Tu We Th Fr Sa    Su Mo Tu We Th Fr Sa  
                   1       1  2  3  4  5  6  
 2  3  4  5  6  7  8      7  8  9 10 11 12 13  
 9 10 11 12 13 14 15     14 15 16 17 18 19 20  
16 17 18 19 20 21 22     21 22 23 24 25 26 27  
23 24 25 26 27 28 29     28 29 30 31           
30                                           

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.