Hitachi

Job Management Partner 1 Version 10 Job Management Partner 1/Advanced Shell Description, User's Guide, Reference, and Operator's Guide


paste command (concatenates multiple files in lines)

Organization of this page

Format

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.

An end-of-line code is output at the end of the concatenated lines. The following is output as the end-of-line code:

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.
  1. 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.

  2. 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.

  3. 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.

  4. 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.
  1. Joins all lines in file1 with separators and then outputs the results.

  2. Outputs only an end-of-line code because file2 is an empty file.

  3. Joins all lines in file3 with separators and then outputs the results.

  4. 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:

  1. Line input from file1 (read from the standard input)

  2. Line input from file2

  3. 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.
  1. The command joins the following values with tab characters and then outputs the results:

    [Figure] Contents of the line read from the standard input (contents of the first line in file1)

    [Figure] Contents of the first line in file2

    [Figure] Contents of the line read from the standard input (contents of the second line in file1)

  2. The command joins the following values with tab characters and then outputs the results:

    [Figure] Contents of the line read from the standard input (contents of the third line in file1)

    [Figure] Contents of the second line in file2

    [Figure] Contents of the line read from the standard input (contents of the fourth line in file1)

  3. The command joins the following values with tab characters and then outputs the results:

    [Figure] Contents of the line read from the standard input (contents of the fifth line in file1)

    [Figure] Contents of the third line in file2

    [Figure] 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)

  4. The command joins the following values with tab characters and then outputs the results:

    [Figure] 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)

    [Figure] Contents of the fourth line in file2

    [Figure] 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.
  1. Joins all lines read from the standard input (all lines in file1) with separators, and then outputs them.

  2. Joins all lines read from file2 with separators, and then outputs them.

Return code

Return code

Meaning

0

Normal termination

1

Error termination

  • At least one of the files specified in the argument failed to open.

    If the -s option was omitted, the files are not concatenated.

    If the -s option was specified, an error message is output when a file open error occurs, and then the next file is processed.

2

Error termination

  • An invalid option was specified.

3

Error termination

  • An unresumable error occurred, such as a memory shortage.

Notes

Usage examples