Thursday, November 19, 2015

Linux paste command

Some time ago ,in some blog page on the Internet, I found this interesting Linux command that allows to merge lines from multiple files and generate lines with content from these files.

paste can be used both on a single file or on multiple files.




Using paste on a single file :


 1. paste command without any options is as good as the cat command when operated on a single file. 
$ paste file1
Linux
Unix
Solaris
HPUX
AIX
2. Join all lines in a file
$ paste -s file1
Linux   Unix    Solaris HPUX    AIX
-s option of paste joins all the lines in a file. Since no delimiter is specified, default delimiter tab is used to separate the columns.

3. Join all lines using the comma delimiter
$ paste -d, -s file1
Linux,Unix,Solaris,HPUX,AIX
-d option is used to specify the delimiter. Using this -d and -s combination, all the lines in the file get merged into a single line.

 4. Merge a file by pasting the data into 2 columns
$ paste - - < file1
Linux   Unix
Solaris HPUX
AIX
The '-' reads a line from the standard input. Two '-' reads 2 lines and pastes them side by side.

 5.Merge a file by pasting the data into 2 columns using a colon separator: 
$ paste -d':' - - < file1
Linux:Unix
Solaris:HPUX
AIX:
        This is same as joining every 2 lines in a file.
  
6. Merge a file by pasting the file contents into 3 columns: 
$ paste - - - < file1
Linux   Unix    Solaris
HPUX    AIX
7. Merge a file into 3 columns using 2 different delimiters: 
$ paste -d ':,' - - - < file1
Linux:Unix,Solaris
HPUX:AIX,
The -d option can take multiple de-limiters. The 1st and 2nd columns is separated by ':', whereas the 2nd and 3rd are separated by a ','.

Using paste on multiple files:

Let us consider a file, file2, with the following contents: 
$ cat file2
Suse
Fedora
CentOS
OEL
Ubuntu
8. paste contents of 2 files side by side
$ paste file1 file2
Linux   Suse
Unix    Fedora
Solaris CentOS
HPUX    OEL
AIX     Ubuntu
paste command is used in scenarios to merge multiple files side by side. As shown above, the file contents are pasted side by side.

 9. paste contents of 2 files side by side with a comma separator
$ paste -d, file1 file2
Linux,Suse
Unix,Fedora
Solaris,CentOS
HPUX,OEL
AIX,Ubuntu
10. paste command can take standard input in case of multiple files too
$ cat file2 | paste -d, file1 -
Linux,Suse
Unix,Fedora
Solaris,CentOS
HPUX,OEL
AIX,Ubuntu
Like this as well: 
$ cat file1 | paste -d, - file2
Linux,Suse
Unix,Fedora
Solaris,CentOS
HPUX,OEL
AIX,Ubuntu
One more: 
$ cat file1 file2 | paste -d, - -
Linux,Unix
Solaris,HPUX
AIX,Suse
Fedora,CentOS
OEL,Ubuntu
11. Read lines in both the files alternatively
$ paste -d'\n' file1 file2
Linux
Suse
Unix
Fedora
Solaris
CentOS
HPUX
OEL
AIX
Ubuntu
Using the newline character as the delimiter, we can read 2 files line by lien alternatively.

See below:

Let us consider 2 sample files with the following contents:
$ cat file1
Unix
Linux
Solaris
$ cat file2
AIX
HPUX
Cygwin
1. The paste command is the easiest of all to read 2 files simultaneously.
$ paste -d"\n" file1 file2
Unix
AIX
Linux
HPUX
Solaris
Cygwin
     paste prints the contents of multiple files side by side. By using the delimiter(-d) as the newline, the lines come to the new line.

No comments:

Post a Comment