Skip to main content

The Linux `paste` Command: Merging Lines of Files

The paste command in Linux is a versatile utility for merging lines of files. It concatenates the corresponding lines of the given files, separated by a tab space by default. This tool is especially useful when dealing with data in tabular form or when columns need to be combined from multiple files.

Syntax

The basic syntax of the paste command is as follows:

paste [OPTION]... [FILE]...
  • [OPTION]... represents the various options that can be applied to the paste command (a few of which are detailed below).
  • [FILE]... represents one or more files whose contents you want to merge.

Options

Here is a table of some common options for the paste command:

OptionShorthandDescription
--serial-sPaste one file at a time instead of in parallel.
--delimiter=DELIM-d DELIMUse DELIM instead of TAB for field delimiter.
--helpDisplay a help message and exit.
--versionOutput version information and exit.

Examples

Before running these examples, let's create a couple of text files using vim.

Creating Files

  1. File 1: names.txt

    To create the first file, run:

    vim names.txt

    Press i to switch to insert mode and enter the following content:

    John
    Jane
    Doe

    Save and exit by pressing Esc, typing :wq, and hitting Enter.

  2. File 2: ages.txt

    To create the second file, run:

    vim ages.txt

    Insert the following content in insert mode:

    25
    30
    22

    Save and exit as before.

Example 1: Simple Merge

To merge the contents of names.txt and ages.txt side by side:

paste names.txt ages.txt

Output:

John  25
Jane 30
Doe 22

Each line from names.txt is merged with the corresponding line from ages.txt using a tab space.

Example 2: Serial Merge

If you want to merge the contents of the same files one after the other rather than in parallel:

paste -s names.txt ages.txt

Output:

John
Jane
Doe
25
30
22

The -s option pastes the contents of each file serially.

Example 3: Using Delimiters

You can specify a delimiter other than the default tab:

paste -d ',' names.txt ages.txt

Output:

John,25
Jane,30
Doe,22

Here, -d ',' option specifies a comma as the delimiter.

Example 4: Combining Multiple Delimiters

The paste command allows specifying a list of delimiters which will be used cyclically:

paste -d ',|' names.txt ages.txt

Output:

John,25
Jane|30
Doe,22

The delimiters , and | are used alternatively for each line.

Example 5: Pasting Multiple Times

You can use the same file to paste its content side by side:

paste names.txt names.txt

Output:

John  John
Jane Jane
Doe Doe

Combining paste with Other Commands

paste can be combined with other Linux commands for more complex operations. For example, you can sort the merged content directly:

paste names.txt ages.txt | sort

Or you can use it with cut to only display certain columns:

paste names.txt ages.txt | cut -f1

Output:

John
Jane
Doe

This will display only the first field of the merged content.

By understanding and using the paste command, you can efficiently manage columnar data in the Linux command line environment, whether you're preparing data for reports, combining logs, or formatting output for further processing.

What Can You Do Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.

YouTube @cloudaffle