How to Use the Paste Command on Linux?


Introduction

The Paste command in Linux is a useful tool for merging multiple files or lines of text into a single file or output. It is commonly used in conjunction with the cut command, which allows users to cut specific lines or sections of text and paste them elsewhere. In this article, we'll explore the various options and usage scenarios for the paste command in Linux, as well as demonstrate how to use it with examples and output.

What is the Paste Command?

The Paste command is a utility on Unix and Linux operating systems that allows users to merge multiple files or lines of text into a single output. Reads from standard input and writes to standard output, making it easy to use in conjunction with other commands and utilities. The ‘paste’ command is often used in conjunction with the cut command, which allows users to cut specific lines or sections of text and paste them elsewhere.

Syntax and Options

The basic syntax of the paste command is as follows −

$ paste [options] [file1-name] [file2-name] ...

Some common options for the paste command include −

  • -d or --delimiters=LIST − This option allows users to specify a list of delimiters to use when merging files. Delimiters will be used to separate lines of text in each file.

  • -s or --serial – This option causes the paste command to merge files in series, rather than in parallel. This means that the first line of the first file will be combined with the first line of the second file and so on.

  • -u or --uniq – This option causes the paste command to remove any duplicate lines when merging files.

Example of Paste Command

Now that we have a basic understanding of the paste command, let's look at some examples of how it can be used. Merging two files

A common use of the paste command is to merge two files into a single output. For example, suppose we have two files, file1.txt and file2.txt, that we want to merge into one file. We can use the paste command as follows −

$ paste file1.txt file2.txt

This will output the contents of both files, with the lines of each file separated by a tab character.

For example, Let's say we have two files, file1.txt and file2.txt, that contain the following data −

File1.txt

apple
banana
orange

File2.txt

red
yellow
orange

If we want to merge these two files vertically, we can use the paste command as follows

$ paste file1.txt file2.txt

This will produce the following output

apple red
banana yellow
orange orange

Using a Custom Delimiter to Merge Files with Paste Command

We can also use the ‘-d’ option to specify a custom delimiter to use when merging files. For example, suppose you want to use a comma (,) as a delimiter instead of a tab character. We can use the paste command as follows −

$ paste -d , file1.txt file2.txt

This will output the contents of both files, with the lines in each file separated by a comma.

Merge Files in Series with Paste Command

By default, the paste command merges files side by side, which means that the first line of the first file is merged with the first line of the second file, and so on. However, we can use the ‘-s‘ option to merge serial files together. For instance −

$ paste -s file1.txt file2.txt

This will output the contents of both files, with lines from each file merged one after the other, rather than in parallel.

Remove Duplicate Rows with Paste Command

Finally, we can use the ‘-u’ option to remove any duplicate lines when merging the files. For instance −

$ paste -u file1.txt file2.txt

This will output the contents of both files, with duplicate lines removed.

Conclusion

In conclusion, the paste command is a useful tool for merging multiple files or lines of text into a single output on Linux. It has several options that allow users to customize the merging process, such as specifying a custom delimiter or merging files serially. It is commonly used in conjunction with the cut command, which allows users to cut specific lines or sections of text and paste them elsewhere. With these options and usage scenarios in mind, you should now have a good understanding of how to use the paste command in Linux.

Updated on: 17-Jan-2023

245 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements