paste Command in Linux | Explained

In Linux, the paste command does not mean copying and pasting the text or a command. However, this command is used to combine and print the files simultaneously horizontally. The “paste” command merges each line of the specified files parallelly and is separated by the delimiter. By default, the delimiter is a “tab” character in the paste command.

The main purpose of this article is to provide complete knowledge about the “paste” command in Linux. The guideline of this article is as follows:

Let’s get into the basics of the paste command.

What is the paste Command in Linux?

The working and usage of the “paste” command depend upon its syntax, specified below.

Syntax

The general structure of the “paste” command is typed below:

$ paste [OPTION]... [FILES]...

In the syntax of the “paste” command, “paste” is the main keyword. However, the square brackets show its supported “options” and the specific files.

The commonly used options of the “paste” command can be seen in the following output.

$ paste --help

How to Use paste Command in Linux?

In this section, various practical examples are described to show the working and usage of the “paste” command using its supported options.

So let’s move on to the first example.

Example 1: Join Files Content

The “paste” command joins the content of more than one file in parallel. Suppose there are two files with the names “file1” and “file2”, as shown in the below screenshots:

$ cat file1.txt $ cat file2.txt

Type the “paste” command in the terminal with the “file1” and “file2”:

$ paste file1.txt file 2.txt

The output shows that the “paste” command has merged the “file1” and “file2” content horizontally.

Example 2: Use the Delimiter to Merge Files

By default, the “paste” command combines the file content with the “tab” delimiter. But the default delimiter can also be replaced with the “-d” option. For example, run the “paste -d” command with the “|(pipe)” symbol as a delimiter:

$ paste -d “|” file1.txt file 2.txt

The above output represents that the “file1” and “file2” content is merged horizontally via “|(pipe)” symbol.

Tip: The “-d” flag allows the user to use more than one symbol or character as a “delimiter” to merge the files at a time. In the below example, the “:(colon)” and “,(comma)” is used for merging the “file1”, “file2”, and “file3” content:

$ paste -d “:,” file1.txt  file2.txt  file3.txt

Example 3: Display the Merge Files Content Sequentially

The “-s” option of the “paste” command scans each file’s content and merges it into a single line. A file’s lines are separated by the “tab” character by default. In case of more than one file, each file is separated by the next line:

$ paste -s file1.txt file 2.txt file3.txt

In the above output, “file1”, “file2”, and “file3” are merged into sequential order. Each line of “file1”, “file2”, and “file3” is separated by a “tab” delimiter, and all the files are separated by a single line.

Example 4: Merge Files Sequentially Using a Delimiter

The combination of “-s” and the “-d” argument with the “paste” command separates each file line with the specified delimiter. In this example, the “file4” and “file5” contains the list of input and output devices as shown below:

$ cat file4.txt
$ cat file5.txt

Execute the “paste” command with the combination of “-s” and “-d” to merge the “file4” and “file5” in a sequential manner using the “:(Colon)” delimiter:

$ paste -s -d “:” file4.txt file5.txt

Example 5: Merge the Nth Lines of a File

The “paste” command is also beneficial to merge the Nth consecutive lines into a single line from a file. For this purpose, the “-(hyphen)” symbol is used to specify the number of lines. Let us understand with an example. As the “file5.txt” holds the following content:

$ cat file5.txt

Use the “paste” command with three “hyphens” to merge every three lines of a file into one line as shown in the screenshot:

$ cat file5.txt | paste - - -

The output displays that “file5.txt” is split into two parts and each part holds the three lines.

That’s all about the paste command in Linux.

Conclusion

The “paste” command helps to merge the files using the “tab” delimiter by default. The “paste” command prints the merged file’s content parallel. Moreover, the delimiter can also be changed by utilizing the “-d” option of the “paste” command. This post has briefly explained the basics, working, and usage of the “paste” command in Linux.