paste Command
Purpose
Joins the lines of different files.
Syntax
Description
The paste command reads input from the files that are 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.
- The paste command supports up to 32767 input files (the
OPEN_MAX
constant). - 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. - 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:
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
- To paste several columns of data together, enter:
This creates a file namedpaste names places dates > npd
npd
that contains the data from thenames
file in one column, theplaces
file in another, and thedates
file in a third. If thenames
,places
, anddates
file look like:
then thenames places dates rachel New York February 5 jerry Austin March 13 mark Chicago June 21 marsha Boca Raton July 16 scott Seattle November 4
npd
file contains:
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.rachel New York February 5 jerry Austin March 13 mark Chicago June 21 marsha Boca Raton July 16 scott Seattle November 4
- To separate the columns
with a character other than a tab, enter: This alternates ! and @ as the column separators. If the
paste -d"!@" names places dates > npd
names
,places
, anddates
files are the same as in example 1, then thenpd
file contains:rachel!New York@February 5 jerry!Austin@March 13 mark!Chicago@June 21 marsha!Boca Raton@July 16 scott!Seattle@November 4
- To display the standard
input in multiple columns, enter:
This lists the current directory in four columns. Eachls | paste - - - -
-
(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:
This example fills the columns across the page with subsequent lines from the standard input. Thels | paste -d"\t\t\t\n" -s -
-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. |