Table of Contents
Introduction to paste command
paste command is used to merge lines of files in the Linux system. It prints the corresponding lines from each file sequentially. Each line is separated by TABS and parallel to each other. With no file, or when the specified file is -, paste reads standard input and prints it.
Different examples to use paste command
You can use paste command to merge lines of a single file or multiple files.
The basic syntax of paste command is:
$ paste [option] file
Now, let's see some practical examples of using paste command in the Linux system. Some of the supported options are:
- -d, --delimiters=LIST : reuse characters from LIST instead of TABs
- -s, --serial: paste one file at a time instead of in parallel
- --help : display this help and exit
- --version : output version information and exit
1. paste command to view the content of file
When paste command is used in a single file, it prints the content of a file.
$ paste file
Sample Output:
2. paste command to merge lines of files
You can merge lines of files by specifying the file names after the paste command. By default, each line of files is separated by TAB.
$ paste file1 file2
Sample Output:
You can also merge the lines of the same file.
3. paste command to paste one file at a time
paste command merges the corresponding lines sequentially and prints them in parallel. -s
or --serial
option helps you to merge one file at a time.
$ paste -s file
OR
$ paste --serial file
Sample Output:
For example, it merges the line of one file, then another file, and so on.
4. paste command to merge lines of a single file
If we use -s
or --serial
option with a single file, it merges all the lines of a file.
$ paste -s file
OR
$ paste --serial file
5. Use different delimiter with paste command
-d
or --delimiters
option allows you to specify the character for the delimiter field. The default delimiter field is TAB.
$ paste -d LIST file
OR
$ paste --delimiters=LIST file
Sample Output:
As you can see, we have used hyphen(-) to separate the line.
6. paste command to print the line in N columns
Normally, paste command prints the line of one file in one column. We can use hyphens(-) to increase the number of columns. For example, for three columns, you can use three hyphens like below.
$ paste - - - < file
Sample Output:
7. Use paste command without specifying file
When no filename is given or filename is given as -, paste reads from the standard input and prints it.
$ paste
OR
$ paste -
Sample Output:
It prints the input until you terminate the command.
Conclusion
We hope this tutorial has helped you to learn paste command. If you still have any confusion, please feel free to ask us in the comment section.
What’s Next
https://www.golinuxcloud.com/cut-command-in-linux/
Further Reading