Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

paste - merge corresponding or subsequent lines of files

&pagelevel(4)&pagelevel

paste can be used to horizontally merge the n-th corresponding lines from two or more input files (parallel merging, Format 1) or to successively merge all lines within a single file (serial merging, Format 2). The result is written to standard output.


Syntax


Format 1:paste[ -d list] file ...
Format 2:paste -s[ -d list] file ...



Joining the n-th lines of several files (parallel merging)
Format 2:paste -s[ -d list] file ...


paste concatenates the n-th line of each input file, treating each file as a column or columns of a table. The corresponding lines are pasted together horizontally and displayed on the standard output (see Example 1).

No option specified

The tab character acts as a delimiter between columns

-d list

(delimiter)

Uses a character from list as a delimiter between output columns.

The characters in list are used consecutively and circularly, i.e. paste returns to the top of the list after using the last character in it. In parallel merging, lines from the last input file are always terminated with a newline character, not with a character from the list.

Any string of arbitrary characters can be specified for list. The following escape sequences may also be used: \n (newline), \t (tab), \\ (backslash), and \0 (empty string, not a null character). If list contains escape sequences, blanks, or shell metacharacters, it must be enclosed in double quotes "...".

file

Name of the input file. This format of paste is only meaningful if you specify several files.

If you use a dash (-) as the name for file, paste reads from standard input.



Joining successive lines (serial merging)
Format 2:paste -s[ -d list] file ...


-s

(subsequent)

For each input file, paste joins the lines together to form a single line and writes this line to standard output. By default, the lines from the input file are separated by tabs (see option d). Each output line is terminated by a newline character.

-d list

(d - delimiter)

One of the characters from list is used in the output line instead of a tab to mark the joins between the input lines.

The characters in list are used consecutively and circularly, i.e. paste returns to the top of the list after using the last character in it.

Any string of arbitrary characters can be specified for list. The following escape sequences may also be used: \n (newline), \t (tab), \\ (backslash), and \0 (empty string, not a null character). If list contains escape sequences, blanks, or shell metacharacters, it must be enclosed in double quotes "...".

file

Name of the input file. You may name more than one file.
If you use a dash (-) as the name for file, paste reads from standard input.

Error

paste: line too long

Output lines must not exceed 511 characters.


paste: too many files - limit 12

A maximum of 12 input files may be specified in format 1


paste: cannot open file : no such files or directory
file does not exist.


paste: cannot open file : Permission denied
User does not have read permission for file.

Locale

The following environment variables affect the execution of paste:

LANG

Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined.

LC_ALL

If set to a non-empty string value, override the values of all the other internationalization variables.

LC_CTYPE

Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other.

LC_MESSAGES

Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.

NLSPATH

Determine the location of message catalogs for the processing of LC_MESSAGES.

Examples of Format 1


Example 1

The corresponding lines from the files numbers and letters are to be pasted together:

The numbers file contains numbers from 1 to 100:

  1
  2
  3
  .
  .
100

The letters file contains lowercase letters from a to z:

a
b
c
.
.
z


$ paste number letters
  1     a

  2     b

  3     c
  .     .

  .     .

 25     y
 26     z

 27

  .
  .

100

Example 2


The current directory contains the following files:

$ ls -C

corr         jokes            names            plan           probe

prog.c       tst              words

The following command numbers these files (see the numbers file in Example 1):

$ ls | paste numbers -

  1     corr

  2     jokes
  3     names

  4     plan

  5     probe
  6     prog.c

  7     tst

  8     words
  9

 10

  :
100

Example 3

The current directory contains the same files as in Example 2. The following command lists the contents of the current directory in three columns. However, the columns will only be justified properly if the individual file names do not extend beyond the next tab stop.

$ ls | paste - - -

corr    jokes   names

plan    probe   prog.c
tst     words

How is this output produced? Compare the above command with the command

$ paste file1 file2 file3

In this case, paste first reads the initial lines from all three files and pastes them into one line. When this is done, the second lines are read, etc.

In the command ls | paste - - -, the first file name that paste reads from standard input (corr) corresponds to the first line from file1; the second file name jokes corresponds to the first line from file2 etc.

Example 4

The current directory contains the same files as in example 2. As in example 3, you now wish to display the file names in three columns, but the second and third columns are to be separated by a colon instead of a tab.

$ ls | paste -d"\t:" - - -
corr    jokes:names

plan    probe:prog.c

tst     words:

Example of Format 2

Example 5

The customers file contains the following:

hansen

smith

cologne

koch

schulz

london

tornio

meyer

perth

$ paste -s customers

hansen  smith   cologne koch    schulz  london  tornio  meyer   perth

The following command joins only three lines of the customers file at a time. This is because a newline character is specified as a delimiter after every third input line:

$ paste -s -d"\t\t\n" customers

hansen  smith   cologne
koch    schulz  london

tornio  meyer   perth

See also

cut, grep, pr