paste Command

Purpose

Joins the lines of different files.

Syntax

paste-s ] [  -d List ] File1 ...

Description

The paste command reads input from the files specified on the command line. The command reads from standard input if a - (minus sign) appears as a file name. The command concatenates the corresponding lines of the given input files and writes the resulting lines to standard output.

By default, the paste command treats each file as a column and joins them horizontally with a tab character (parallel merging). You can think of the paste command as the counterpart of the cat command (which concatenates files vertically, that is, one file after another).

With the -s flag, the paste command combines subsequent lines of the same input file (serial merging). These lines are joined with the tab character by default.

Notes:
  1. The paste command supports up to 32767 input files (the OPEN_MAX constant).
  2. The action of the pr -t -m command is similar to that of the paste command, but creates extra spaces, tabs, and lines for a nice page layout.
  3. Input files should be text files, but may contain an unlimited number of line lengths.

Flags

Item Description
-d List Changes the delimiter that separates corresponding lines in the output with one or more characters specified in the List parameter (the default is a tab). If more than one character is in the List parameter, then they are repeated in order until the end of the output. In parallel merging, the lines from the last file always end with a new-line character instead of one from the List parameter.

The following special characters can also be used in the List parameter:

\n
New-line character
\t
Tab
\\
Backslash
\0
Empty string (not a null character)
c
An extended character

You must put quotation marks around characters that have special meaning to the shell.

-s Merges subsequent lines from the first file horizontally. With this flag, the paste command works through one entire file before starting on the next. When it finishes merging the lines in one file, it forces a new line and then merges the lines in the next input file, continuing in the same way through the remaining input files, one at a time. A tab separates the lines unless you use the -d flag. Regardless of the List parameter, the last character of the file is forced to be a new-line character.

Exit Status

This command returns the following exit values:

Item Description
0 Successful completion.
>0 An error occurred.

Examples

  1. To paste several columns of data together, enter:
    paste names places dates > npd
    This creates a file named npd that contains the data from the names file in one column, the places file in another, and the dates file in a third. If the names, places, and dates file look like:
    names           places          dates
    rachel          New York        February 5
    jerry           Austin          March 13
    mark            Chicago         June 21
    marsha          Boca Raton      July 16
    scott           Seattle         November 4
    then the npd file contains:
    rachel          New York        February 5
    jerry           Austin          March 13
    mark            Chicago         June 21
    marsha          Boca Raton      July 16
    scott           Seattle         November 4
    A tab character separates the name, place, and date on each line. These columns do not always line up because the tab stops are set at every eighth column.
  2. To separate the columns with a character other than a tab, enter:

    paste  -d"!@" names places dates > npd

    This alternates ! and @ as the column separators. If the names, places, and dates files are the same as in example 1, then the npd file contains:
    rachel!New York@February 5
    jerry!Austin@March 13
    mark!Chicago@June 21
    marsha!Boca Raton@July 16
    scott!Seattle@November 4
  3. To display the standard input in multiple columns, enter:
    ls | paste - - - -
    This lists the current directory in four columns. Each - (minus) tells the paste command to create a column containing data read from the standard input. The first line is put in the first column, the second line in the second column, and so on.

    This is equivalent to:

    ls | paste  -d"\t\t\t\n" -s -
    This example fills the columns across the page with subsequent lines from the standard input. The -d"\t\t\t\n" defines the character to insert after each column: a tab character (\t) after the first three columns, and a new-line character (\n) after the fourth. Without the -d flag, the paste -s - command would display all of the input as one line with a tab character between each column.

Files

Item Description
/usr/bin/paste Contains the paste command.