Sep 23, 2023 3 min read

Paste Command in Linux (Merge Lines)

In this tutorial, you will learn about the Paste Command in Linux. The paste command allows you to horizontally merge lines of files.

Paste Command in Linux (Merge Lines)
Table of Contents

Introduction

Before we begin talking about Paste Command in Linux (Merge Lines), let's briefly understand – What is paste command?

paste is a command that allows you to horizontally merge lines of files. It outputs lines separated by tabs that contain the sequentially corresponding lines of each file specified as an argument.

In this tutorial, you will learn about the Paste Command in Linux. We will also address a few FAQs on Paste Command in Linux(Merge Lines).

How to Use the paste Command

paste is one of the lesser-known and used command-line utilities in Linux and Unix.

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

Paste uses the standard input if no input files are provided or when - is used as an argument.

Suppose we have the following files:

Iron Man
Thor
Captain America
Hulk
Spider Man
Black Widow
Captain Marvel
Dark Phoenix
Nebula

Without an option, paste will read all files provided as arguments and join their corresponding lines horizontally, separated by space.

paste file1 file2
Output

Iron Man	Black Widow
Thor	Captain Marvel
Captain America	Dark Phoenix
Hulk	Nebula
Spider Man

The > and >> operators can be used to direct output to a file rather than the screen:

paste file1 file2 > file3

If the file does not already exist, one will be created. The > operator replaces an existing file, whereas the >> operator appends the output to the file.

Instead of using the TAB separator by default, you can provide a list of characters to be used as delimiters by using the -d, -delimiters option.

Each delimiter is used in turn. When the list is finished, paste returns to the first delimiter character.

To use the (underscore) character instead of TAB as a delimiter, type:

paste -d '_' file1 file2
Output

Iron Man_Black Widow
Thor_Captain Marvel
Captain America_Dark Phoenix
Hulk_Nebula
Spider Man_

Here is an illustration of how to use two delimiters:

paste -d '%|' file1 file2 file1

The first character from the delimiters list is used to separate the lines from the first and second files. The second delimiter is used to separate the second and third file lines.

If more files are supplied, paste will restart from the beginning of the list.

Output

Iron Man%Black Widow|Iron Man
Thor%Captain Marvel|Thor
Captain America%Dark Phoenix|Captain America
Hulk%Nebula|Hulk
Spider Man%|Spider Man  

The -s, —serial option instructs paste to display one file's lines at a time rather than one line from each file.

paste -s file1 file2

The command will merge the following lines from the given file:

Output

Iron Man	Thor	Captain America	Hulk	Spider Man
Black Widow	Captain Marvel	Dark Phoenix	Nebula

When used with the -z, --zero-terminated option, paste delimits the items with a null character rather than the default newline character. This behavior is useful when using paste in conjunction with the find -print0 and xargs-0 commands to handle file names that contain special character.

FAQs to paste command in Linux (Merge Lines)

Can I merge multiple files into one using the "paste" command?

Yes, you can merge multiple files into one using the "paste" command by specifying the file names as arguments after the command. By default, it will merge the files horizontally.

How can I merge files vertically instead of horizontally?

To merge files vertically, use the -s option with the "paste" command as follows: paste -s [FILE1] [FILE2] ...

Can I specify a different delimiter when merging lines with the "paste" command?

Yes, you can specify a custom delimiter using the -d option followed by the desired delimiter. For example, to use a comma as the delimiter, you would use the command: paste -d ',' [FILE1] [FILE2] ...

How can I merge lines only from specific columns of input files?

You can achieve this by using the -d option to specify a delimiter and the -f option followed by the column number(s) or range(s) you want to merge. For example, to merge lines from the second and third columns, you would use: paste -d ',' -f 2,3 [FILE1] [FILE2] ...

Is it possible to merge lines from files with different numbers of lines?

Yes, the "paste" command handles files with different numbers of lines. If a file runs out of lines, it will fill the empty spots with delimiter characters or blanks.

Can the "paste" command handle large files efficiently?

Yes, the "paste" command is designed to handle large files efficiently. It processes files line by line, which allows it to handle files of any size without consuming excessive memory.

Are there any other useful options available with the "paste" command?

Yes, some other commonly used options include:

  • -d option to specify the delimiter.
  • -s option to merge files vertically.
  • -f option to select specific columns to merge.
  • -z option for zero-based column numbering.
  • -r option to repeat the last specified delimiter.
  • -N option to explicitly specify the number of input lines to merge.
  • --help option to display the command's help documentation.

Conclusion

Corresponding lines of a set of files are combined using the paste command.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.