Read, delimit, and combine files using the PASTE command in Linux

Home > Search > Linux commands
  by

The paste command without any options is identical to the cat command. The paste command without any option can be used to view the contents of a file. 

[root@server1 ]# paste file1
Hello
world
How
are
you
today?

 

The -s (single line) option cab be used to put all of the lines into a single line.

[root@server1 ]# paste -s file1
Hello world How are you today?

 


Delimiting files

The -T (show tabs) option can be used to view the tabs in the file. The ^I is used to represent a tab.

[root@server1 ]# paste -s -T file1
Hello^Iworld^IHow^Iare^Iyou^Itoday?

 

The -d (delimitor) option can be used to delimit a file. For example, to delimit a file with a comma:

[root@server1 ]# paste -d, file1 > newfile

[root@server1 ]# cat newfile
Hello world,How are you today?

 

To delimit a file with a semicolor:

[root@server1 ]# paste -d ';'  file1 > newfile

[root@server1 ]# cat newfile
Hello;world;How;are;you;today?

 


Writing and appending files

Redirection can be used to overwrite file2 with the contents of file1.

[root@server1 ]# paste file1 > file2

 

Redirection can also be used to append the contents of file1 into file2. The \n delimiter is used so that the contents of file1 are appended to a new line in file2.

[root@server1 ]# paste -d '\n' file1 >> file2

 


Combining files

Let's say there are two files, file1 and file2, and each file has a line of text. For example, let's say file1 contains "Hello World" and file2 contains "How are you today?" The paste command can be used to place each line in the two files side by sde.

[root@server1 ]# paste file1 file2
Hello world  How are you today?

 

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments