How To Use Paste Command (Merge Lines)
In this tutorial, we will explain how to use the paste command.
paste is a command that allows you to merge lines of files horizontally. It outputs lines consisting of the sequentially corresponding lines of each file specified as an argument, separated by tabs.
How to Use the paste Command
paste is one of the lesser-known and used Linux and Unix command-line utilities.
The general syntax for the paste command is as follows:
If no input files are provided or when – is given as argument, paste uses the standard input.
Suppose we have the following files:
When invoked without an option paste will read all files given as arguments and horizontally merge the corresponding lines of the files, separated by space:
Instead of displaying the output on the screen, you can redirect it to a file using the
>, >> operators:
If the file doesn’t exist, it will be created. The > operator will overwrite an existing file, while the >> operator will append the output to the file.
The -d, -delimiters option allows you to specify a list of characters to be used as delimiters instead of the default TAB separator.
Each delimiter is consecutively used. When the list is exhausted, paste starts again from the first delimiter character.
To use the _ (underscore) character as a delimiter instead of TAB, you would type:
Here is an example of using two delimiters:
The lines from the first and the second file are separated with the first character from the delimiters list. The second and the third file lines are separated with the second delimiter.
If more files were given, paste starts again from the beginning of the list.
The -s, –serial option tells paste to display the lines of one file at a time instead of one line from each file.
The command will merge all lines from the given file in separated lines:
When used with the -z, –zero-terminated option, paste uses a null character to delimit the items instead of the default newline character. This behavior is handy when paste is used in combination with find -print0 and xargs -0 commands to handle file names containing special characters.
The paste command is used to merge corresponding lines of given files.
If you have any questions or feedback, feel free to leave a comment.