Paste command examples in Linux/Mac OS

The Linux command paste is one of the powerful commands that can be used to “merge multiple files in to a single file” and also “collapse multiple lines in a single file to a new file”. Here are some basic examples of using the paste command.

Paste command to merge multiple files into a single file

Let us say you have two files.

>cat last_names.txt
Smith
Jones
>cat first_names.txt
John
David 

We can use paste command to merge each line in these two files into a single file

>paste last_names.txt first_names.txt
Smith John
Jones David

By default, paste commands merges two file with tab delimiter. We can also specify a delimiter with using “-d” option. For example, to use comma as delimiter

>paste -d"," last_names.txt first_names.txt
Smith,John
Jones,David

Paste command to join N lines of a file

paste command can also be used with single file (instead of multiple) to join multiple lines from a file. Let us say we have a file like this

>cat numbers.txt
One
Two
Three
Four
Five
Six

We can use paste command to merge multiple lines, say every three lines, into a single line

>less numbers.txt | paste - - - 
One Two Three
Four Five Six

The less command on the file feeds the content of the file numbers.txt to the paste
command. The three dashes “- – -” in the above paste command merges three lines. If we want
to merge every two lines, we will use two dashes like this

>less numbers.txt | paste - -  
One Two 
Three Four
Five Six