The paste command is used to merge lines from text files horizontally. Each line of an initial file is a row in the first column; using paste, you specify a second file, and every line of the second file becomes a row in a new, second column. By default, the paste command uses a tab space delimiter to separate each column. You can use the -d option to specify a different delimiter.
For example, you have a file named cities:
# cat file1 Mumbai Pune Delhi Chennai
You also have a second file named countries:
# cat file2 India China UK USA
The output of paste -d , cities countries is as follows:
# paste file1 file2 Mumbai,India Pune,China Delhi,UK Chennai,USA
If you encounter the below error while running the cut command:
cut: command not found
you may try installing the below package as per your choice of distribution:
Distribution | Command |
---|---|
OS X | brew install coreutils |
Debian | apt-get install coreutils |
Ubuntu | apt-get install coreutils |
Alpine | apk add coreutils |
Arch Linux | pacman -S coreutils |
Kali Linux | apt-get install coreutils |
CentOS | yum install coreutils |
Fedora | dnf install coreutils |
Raspbian | apt-get install coreutils |
paste Command Examples
1. Join all the lines into a single line, using TAB as delimiter:
# paste -s file
2. Join all the lines into a single line, using the specified delimiter:
# paste -s -d delimiter file
3. Merge two files side by side, each in its column, using TAB as delimiter:
# paste file1 file2
4. Merge two files side by side, each in its column, using the specified delimiter:
# paste -d delimiter file1 file2
5. Merge two files, with lines added alternatively:
# paste -d '\n' file1 file2