8.4.24 paste command (concatenates multiple files in lines)
Syntax
paste[-s][-d list][path-name ...]
Description
This command concatenates multiple files in units of lines and then outputs the results to the standard output. You can also join all lines in a file into a single continuous line and then concatenate multiple files.
Arguments
- -s
Joins all lines in the files into a single line (with separators).
If the -s option is omitted, the command joins the lines in all files that have the same line number (with separators).
- -d list
Specifies a list of the separators that will be inserted between the lines to be concatenated. If the -d option is omitted, the tab character is assumed.
To specify a space or tab character, enclose the character in double quotation marks (").
You can also specify special characters as separators.
Permitted special character
Meaning
Remarks
\n
End-of-line code
In Windows, an end-of-line code is denoted by [CR] + [LF].
In UNIX, an end-of-line code is denoted by [LF].
\t
Tab character
--
\\
One backslash character
--
\0
Null character string
Character string with a length of zero (""). No separator is inserted between the lines to be concatenated.
- Legend:
--: Not applicable.
Depending on the shell used to execute the paste command, special characters are treated as escape characters. Therefore, enclose special characters in double or single quotation marks (" or '). If a non-special character is immediately preceded by a backslash (\), the command ignores \ and uses the character following \ as the separator. If only \ is specified, the command terminates with an error.
You can specify multiple separators. When multiple separators are specified, the command handles them as follows:
Each time the command concatenates lines, it fetches a separator and inserts it between the lines. The separators are fetched in order from the beginning of the list.
When the -s option is not specified, the command fetches separators in order from the beginning of the list again after it has output concatenated lines.
When the -s option is specified, the command fetches separators in order from the beginning of the list again after it has joined all lines in a file and has output them.
When the end of the list of separators specified in the -d option is reached, the command fetches separators from the list again in order from the beginning of the list.
- path-name
Specifies the path name of a file to be concatenated and output. If no path name is specified or a hyphen (-) is specified as the path name, the command reads the path name from the standard input.
You can specify multiple path names and hyphens (-) or a mixture of path names and hyphens.
If multiple files are specified and an open error occurs on any of the files, the following occurs:
When the -s option is omitted, the command outputs an error message and terminates with return code 1. In this case, nothing is output to the standard output.
When the -s option is specified, the command outputs an error message for the file resulting in the open error and resumes processing. When the command has processed all files, it terminates with return code 1.
If only one path name is specified, the command runs as follows:
When the -s option is omitted, the command only outputs the lines.
When the -s option is specified, the command joins all lines in the file and then outputs them.
Input and output of lines
In input files, the command treats a record separated by an end-of-line code as one line.
In Windows, [CR] + [LF] or by [LF] is treated as the end-of-line code.
In UNIX, [LF] is treated as the end-of-line code.
If each record in the input file is separated by [CR] + [LF], the concatenated lines contain [CR].
An end-of-line code is output at the end of the concatenated lines. The following is output as the end-of-line code:
In Windows: [CR]+[LF]
In UNIX: [LF]
Concatenating files in units of lines (when the -s option is omitted)
The command joins the lines that have the same line number in all the files and then outputs the results as a single line (with separators). Null lines are treated as null character strings when they are joined with other lines.
If the end-of-file is detected in any of the files while lines with the same line number are being read, any remaining lines for that file are joined as null characters with the lines from the other files.
The following example concatenates file1, file2, file3, and file4 in units of lines.
- Contents of file1:
a001 a002
- Contents of file2:
b001 (null line) b003
- Contents of file3:
c001 c002 c003 c004
- Contents of file4:
d001
- Command that concatenates file1, file2, file3, and file4 in units of lines:
$ paste file1 file2 file3 file4
The command concatenates the files as follows (-> indicates a tab character used as a separator):
a001->b001->c001->d001 1. a002->-> c002-> 2. -> b003->c003-> 3. -> -> c004-> 4.
The command joins the first lines of file1, file2, file3, and file4 and then outputs the results. The command inserts a tab character between the lines.
The command joins the following values with tab characters and then outputs the results:
Contents of the second line of file1
Null character string because the second line of file2 is the null line
Contents of the second line of file3
Null character string because the end of file is reached in file4
A null character string is a character string with a length of 0. Therefore, the actual result that is output is the contents of the second line of file1 + tab character + tab character + contents of the second line of file3 + tab character.
The command joins the following values with tab characters and then outputs the results:
Null character string because the end-of-file is reached in file1
Contents of the third line of file2
Contents of the third line of file3
Null character string because the end of file is reached in file4
The actual result that is output is a tab character + contents of the third line of file2 + tab character + contents of the third line of file3 + tab character.
The command joins the following values with tab characters and then outputs the results:
Null character string because the end-of-file is reached in file1
Null character string because the end-of-file is reached in file2
Contents of the fourth line of file3
Null character string because the end-of-file is reached in file4
The actual result that is output is a tab character + tab character + contents of the fourth line of file3 + tab character.
If any of the specified files is empty, lines in that file are treated as null character strings and joined with other files' lines. However, if all the files specified in the argument are empty, no line is output.
Joining lines in files (when the -s option is specified)
The command joins all lines in one file with separators into a single line and then concatenates it with other files. If a file is empty, the command outputs only an end-of-line code. If all the files specified in the argument are empty, the command outputs an end-of-line code for each file.
The following example concatenates file1, file2, file3, and file4.
- Contents of file1:
a001 a002
- Contents of file2:
Null file
- Contents of file3:
c001 c002 c003 c004
- Contents of file4:
d001
- Command that concatenates file1, file2, file3, and file4:
$ paste -s file1 file2 file3 file4
The command concatenates the files as follows (-> indicates a tab character used as a separator):
a001->a002 1. 2. c001->c002->c003->c004 3. d001 4.
Joins all lines in file1 with separators and then outputs the results.
Outputs only an end-of-line code because file2 is an empty file.
Joins all lines in file3 with separators and then outputs the results.
Outputs the contents of the line because file4 contains only one line.
Joining lines read from the standard input
This subsection explains joining lines that are read from the standard input.
- Concatenating files in units of lines (when the -s option is omitted)
The command reads only one line from the standard input and joins that line with a line from another file. If multiple hyphens (-) are specified, the command reads a line from the standard input for each - specified in order, and then joins the lines.
When a file is to be concatenated with the contents of the standard input, the command keeps reading lines from the standard input until the end-of-file is reached. Therefore, if EOF is read from the standard input, the command treats it as the null character string and joins that null character string with a line from the file.
The following example reads file1 from the standard input and concatenates it with file2 in units of lines.
- Contents of file1:
a001 a002 a003 a004 a005
- Contents of file2:
b001 b002 b003 b004
- Command that concatenates file1 and file2 in units of lines:
$ cat file1 | paste - file2 -
The lines are joined in the following order:
Line input from file1 (read from the standard input)
Line input from file2
Line input from file1 (read from the standard input)
The command concatenates the files as follows (-> indicates a tab character used as a separator):
a001->b001->a002 1. a003->b002->a004 2. a005->b003-> 3. -> b004-> 4.
The command joins the following values with tab characters and then outputs the results:
Contents of the line read from the standard input (contents of the first line in file1)
Contents of the first line in file2
Contents of the line read from the standard input (contents of the second line in file1)
The command joins the following values with tab characters and then outputs the results:
Contents of the line read from the standard input (contents of the third line in file1)
Contents of the second line in file2
Contents of the line read from the standard input (contents of the fourth line in file1)
The command joins the following values with tab characters and then outputs the results:
Contents of the line read from the standard input (contents of the fifth line in file1)
Contents of the third line in file2
Null character string because EOF was read from the standard input (the end of file was reached in the file sent to the standard input)
The command joins the following values with tab characters and then outputs the results:
Null character string because EOF was read from the standard input (the end of file was reached in the file sent to the standard input)
Contents of the fourth line in file2
Null character string because EOF was read from the standard input (the end of file was reached in the file sent to the standard input)
- Joining lines in files (when the -s option is specified)
The command repeats reading one line at a time from the standard input and joins the lines with separators until EOF is read. The command then concatenates the joined lines with another file. The following example reads file1 from the standard input and concatenates it with file2.
- Contents of file1:
a001 a002 a003
- Contents of file2:
b001 b002
- Command that concatenates file1 and file2 (file1 is read from the standard input):
$ cat file1 | paste -s - file2
The command concatenates the files as follows (-> indicates a tab character used as a separator):
a001->a002->a003 1. b001->b002 2.
Joins all lines read from the standard input (all lines in file1) with separators, and then outputs them.
Joins all lines read from file2 with separators, and then outputs them.
Return code
Return code |
Meaning |
---|---|
0 |
Normal termination |
1 |
Error termination
|
2 |
Error termination
|
3 |
Error termination
|
Notes
The paste command expects text files. Input from binary files and output of binary data are not guaranteed to work.
When the -s option is omitted, the command opens all the files specified in the argument simultaneously. In UNIX, an error might occur when files are opened depending on OS settings, such as the maximum number of files that can be open at the same time in the entire OS or the maximum number of file descriptors permitted for a process (ulimit).
Examples
Concatenate multiple files in units of lines. Use the tab character as the separator between the lines that are joined. In the output results, -> indicates the tab character.
Contents of input file file01:
a001 a002 a003
Contents of input file file02:
b001 b002 b003
Contents of input file file3:
c001 c002 c003
The following shows the specified command and the execution results:
$ paste file01 file02 file03 a001->b001->c001 a002->b002->c002 a003->b003->c003
Concatenate multiple files in units of lines. Insert the separators = and % one at a time in this order between the lines that are joined.
Contents of input file file01:
a001 a002 a003
Contents of input file file02:
b001 b002 b003
Contents of input file file03:
c001 c002 c003
Contents of input file file04:
d001 d002 d003
The following shows the specified command and the execution results:
$ paste -d "=%" file01 file02 file03 file04 a001=b001%c001=d001 a002=b002%c002=d002 a003=b003%c003=d003
Concatenate multiple files in units of lines. Insert the separators =, %, and @ one at a time in this order between the lines that are joined.
Contents of input file file01:
a001 a002 a003
Contents of input file file02:
b001 b002 b003
Contents of input file file03:
c001 c002 c003
The following shows the specified command and the execution results:
$ paste -d "=%@" file01 file02 file03 a001=b001%c001# a002=b002%c002 a003=b003%c003
- #
The concatenation count for the lines with the same line number is 2 as shown in the following:
Concatenate one line of file01 and one line of file02
Concatenate one line of file03 and the results of concatenating file01 and file02
Note that the separator @ specified in the -d option is not used.
Read the list of file names displayed with the ls command from the standard input and then output four columns. Insert the comma (,) as the separator between file names.
$ ls a001 a002 a003 a004 b001 b002 b003 b004 c001 c002 $ ls | paste -d "," - - - - a001,a002,a003,a004 b001,b002,b003,b004 c001,c002,,
Join all lines in each file into a single line and then concatenate all files. Insert the separators =, %, and @ one at a time in this order between the lines that are joined.
Contents of input file file01:
a001 a002 a003
Contents of input file file02:
b001 b002 b003 b004
Contents of input file file03:
c001 c002 c003 c004 c005
Contents of input file file04:
d001 d002
The following shows the specified command and the execution results:
$ paste -s -d "=%@" file01 file02 file03 file04 a001=a002%a003 b001=b002%b003@b004 c001=c002%c003@c004=c005 d001=d002
When multiple files are concatenated in units of lines, the following message is displayed if a nonexistent file is specified:
$ paste file01 file02 file03 paste: file02: No such file or directory
Join all lines in each file and concatenate all files. Nonexistent files file01 and file03 are specified as input files.
Contents of input file file02:
b001 b002 b003 b004
Contents of input file file04:
d001 d002
The following shows the specified command and the execution results:
$ paste -s -d "=%@" file01 file02 file03 file04 paste: file01: No such file or directory# b001=b002%b003@b004 paste: file03: No such file or directory# d001=d002
- #
Contents output to the standard error output.