The “paste” Command in Linux [6 Practical Examples]

The paste command in Linux is a helpful tool for merging lines from multiple files. It takes input from multiple files and pastes the corresponding line together, separated by a delimiter. This command is very useful for merging columns of data from multiple files to a single file for processing further. In this article, I will demonstrate the details of the paste command in Linux with practical examples.

A. Description

The paste command in Linux can merge data from multiple files into a single file.

B. Syntax

The paste command in Linux takes OPTION and FILE (as an argument). The syntax for the paste command is given below.

paste [OPTION]... [FILE]...

Note: Here, the OPTION and FILE enclosed by square brackets mean that OPTIONS and FILE are not mandatory for the command. Here, three dots preceded by OPTION and FILE means that the paste command can take multiple OPTIONs and FILEs.

C. Options

Different options can be added to the syntax of the paste command to modify the command. Here, I have listed some valuable options below. Nevertheless, If you do not find your desired option here, you can look for it on the man (manual) page. To go to the man page, type the following command and press ENTER.

man paste

Useful Options

  • -d/–delimiters=LIST: It reuses characters from LIST instead of TABs.
  • -s/–serial: It pastes one file at a time instead of in a parallel manner.
  • -z/–zero-terminated: This option merges files separated by a NULL character instead of a newline character.
  • –help: It displays the help section and then exits.
  • –version: It prints version information and exit.

Note: All options in Linux are case-sensitive. Therefore, you must be careful while using these.

Practical Examples of the “paste” Command in Linux

The paste command is used for working with multiple files. Moreover, the paste command in Linux has many practical applications and a few of them are outlined below. I will work with the following three files in this article: number, country, and currency.The number, country, and currency files contain some data.

Example 1: Merge Multiple Files in Parallel Using the “paste” Command in Linux

You can merge multiple files parallelly with the paste command in Linux. Here, I will merge the number, country, and currency files parallelly. To do so, follow the below procedures.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Then type the following command in the command prompt:

paste number country currency

➌ Now, press the ENTER button.

Output >

The following image shows that the paste command has merged the number, country, and currency files parallelly.The paste command has merged the number, country, and currency files parallelly.


Similar Readings


Example 2: Setting a Delimiter for Merging the Files Using the “paste” Command in Linux

You can set the delimiter you want while merging files using the -d option with the paste command in Linux. Here, I will set the delimiter “|” while merging the number, country, and currency files parallelly. To achieve so, follow the steps given below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Then, copy the following command in the terminal.

paste -d "|" number country currency

➌ Now, tap the ENTER button.

Output >

The following image shows that the paste command in Linux has merged the number, country, and currency files, parallelly setting the “|” (pipe) sign as a delimiter.The paste command has merged the number, country, and currency files, parallelly setting the “|” (pipe) sign as a delimiter.

Example 3: Merge Multiple files in a Sequential Manner

By default, the paste command merges multiple files in a parallel manner. But you can easily merge multiple files sequentially using the -s option with the paste command in Linux. Here, I will merge the number, country, and currency files sequentially. To do this task, follow the below procedures.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Then, type the following command in the command prompt.

paste -s number country currency

➌ Now, press the ENTER button.

Output >

The below image illustrates that I have merged the number, country, and currency files sequentially using the -s option with the paste command in Linux.The number, country, and currency files are merged sequentially using the -s option with the paste command.


Similar Readings


Example 4: Combining Consecutive Lines of a File in a Single Line

You can print some consecutive lines in a single line using the hyphen symbol with the paste command. Besides, the number of hyphens will determine the number of consecutive lines displayed in a single line. Here, I want to print two consecutive lines of the country file in a single line of the terminal. Therefore, I will use two hyphens, “–” after the paste command. To do the same, follow the given instructions.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Then, copy the following command in the command prompt:

cat country | paste - -

➌ Now, press the ENTER button.

Output >

Two consecutive lines of the country file are printed on the terminal using the double hyphen “–” after the paste command, as depicted in the following image.Two consecutive lines of the country file are printed on the terminal using the double hyphen “--” after the paste command.

Example 5: Combining the “paste” Command With Other Commands in Linux

You can use the paste command in Linux with other commands to increase productivity. Here, I will use the cut command with the paste command. Usually, the paste command needs two files. One file named number is given after the paste command, and a hyphen is mentioned after the number file. It will set the contents of the second file named country after (which will be sent from the cut command through the piping command) the contents of the number file. To do so, follow the below procedures.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Then, type the following command in the command prompt:

cut -d " " -f 1 country | paste number -

Note: The cut command will separate the first field of all lines of the country file, then pass the fields to the paste command through the piping command.

➌ Now, press the ENTER button.

Output >

In the below image, you can see that I have used the paste command along with the cut command.The paste command is used along with the cut command.


Similar Readings


Example 6: Display the Version of the “paste” Command in Linux

You can print the version information of the paste command currently running on your system. Here, I will display the version information of the paste command currently running in my system. To achieve so, follow the procedures below.

Steps to Follow >

➊ At first, open the Ubuntu Terminal.

➋ Then, copy the following command in the command prompt:

paste --version

➌ Now, press the ENTER button.

Output >

The following image shows that I have displayed the version information of the paste command on the terminal.The version information of the paste command is displayed on the terminal.

Conclusion

In this article, I have demonstrated the process of merging lines from multiple files using the paste command in Linux. Therefore, I hope you’ll be competent enough to explore more things with the help of these practical examples.


Similar Readings

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment