Skip to the content.

Paste Command

The paste command merges lines of files horizontally by combining corresponding lines from each file specified as an argument and separating them with tabs.

How to use the paste Command

The paste command is not as commonly used as other Linux and Unix command-line utilities, but it can be incredibly useful. The paste command has the following syntax:

paste [OPTION]... [FILE]...

If no input files are specified or if the - argument is used, paste will default to using standard input.

Example Usage

vi file1.txt

paste followings inside of file1.txt

1
2
3
4
5
6
7
vi file2.txt

paste followings inside of file2.txt

One
Two
Three
Four
Five
Six
Seven
paste file1.txt file2.txt
1	One
2	Two
3	Three
4	Four
5	Five
6	Six
7	Seven

For example, you can use the collon character as a delimiter by typing:

paste -d ":" file1.txt file2.txt

This will merge corresponding lines of file1.txt and file2.txt using collon as the delimiter and the output will be:

1:One
2:Two
3:Three
4:Four
5:Five
6:Six
7:Seven
vi file3.txt

paste followings inside of file3.txt

I
II
III
IV
V
VI
VII
paste -d ":-" file1.txt file2.txt file3.txt
1:One-I
2:Two-II
3:Three-III
4:Four-IV
5:Five-V
6:Six-VI
7:Seven-VII
paste -s file2.txt
One	Two	Three	Four	Five	Six	Seven
paste -s file1.txt file2.txt file3.txt
1	2	3	4	5	6	7
One	Two	Three	Four	Five	Six	Seven
I	II	III	IV	V	VI	VII

For example, suppose we have two files named demo1.txt and demo2.txt with the following contents:

vi demo1.txt

paste followings inside of demo1.txt

Hello, world!
This is the first file.
vi demo2.txt

paste followings inside of demo2.txt

This is the second file.
It contains special characters like @, #, and $.
paste demo1.txt demo2.txt
Hello, world!	This is the second file.
This is the first file.	It contains special characters like @, #, and $.
paste -z demo1.txt demo2.txt | xargs -0 echo
Hello, world!
This is the first file.
	This is the second file.
It contains special characters like @, #, and $.

Ref: https://twitter.com/linuxopsys/status/1654545922636804100?t=97s5tPqYYkXvlS1kyUBITQ&s=35

Transpose Lines Using paste Command

1 one
2 two
3 three
4 four
5 five
# with *paste* command
cut -d ' ' -f 1 transpose.txt | paste -d ' ' -s && echo && cut -d ' ' -f 2 transpose.txt | paste -d ' ' -s
# result will be:
# 1 2 3 4 5
#
# one two three four five
# with *awk* command
awk '{ORS=NR%5?" ":"\n"; print $1}' transpose.txt ; awk '{ORS=NR%5?" ":"\n"; $1=""; print $0}' transpose.txt | sed 's/^ //'
# result will be:
# 1 2 3 4 5
# one  two  three  four  five