LinuxCommandLibrary

paste

Merge corresponding lines of files

TLDR

Join all the lines into a single line, using TAB as delimiter

$ paste [[-s|--serial]] [path/to/file]
copy

Join all the lines into a single line, using the specified delimiter
$ paste [[-sd|--serial --delimiters]] [delimiter] [path/to/file]
copy

Merge two files side by side, each in its column, using TAB as delimiter
$ paste [path/to/file1] [path/to/file2]
copy

Merge two files side by side, each in its column, using the specified delimiter
$ paste [[-d|--delimiters]] [delimiter] [path/to/file1] [path/to/file2]
copy

Merge two files, with lines added alternatively
$ paste [[-d|--delimiters]] '\n' [path/to/file1] [path/to/file2]
copy

SYNOPSIS

paste [OPTION]... [FILE]...

PARAMETERS

-d LIST, --delimiters=LIST
    Use specified characters from LIST as delimiters, cycling through them for each subsequent column.

-s, --serial
    Paste lines from a single input file onto a single output line. If multiple files are provided, each file's lines are pasted onto a separate output line sequentially.

--zero-terminated
    Treat input and output lines as NUL-terminated strings instead of newline-terminated.

-
    Read input from standard input (stdin) for a file.

DESCRIPTION

The paste command in Linux is used to merge corresponding lines from multiple input files, or multiple lines from a single file, into a single output stream. By default, it joins lines using a tab character as a delimiter. This horizontal concatenation contrasts with commands like cat, which concatenate files vertically.

paste is particularly useful for combining columnar data from different sources, creating new data sets from existing ones, or preparing data for further processing by other utilities. It can read from standard input if a hyphen (-) is specified as a filename. The command supports custom delimiters and also offers a 'serial' mode (-s) for processing lines within a single file or multiple files sequentially onto distinct output lines.

CAVEATS

The default delimiter is a tab character. When merging files in parallel (default behavior), paste outputs lines until all input files are exhausted; shorter files are conceptually padded with empty fields.

When using -s, each file's content is joined into a single line before output. If multiple files are provided with -s, each file's lines are joined into separate output lines.

paste is primarily for simple horizontal concatenation; for more complex joining or field-based operations, commands like join or awk are more suitable.

PARALLEL VS. SERIAL PASTING

Parallel Pasting (default):
When invoked as `paste file1 file2 file3`, it combines the first line of `file1`, `file2`, and `file3` on one output line, then the second line from each, and so on, until all files are exhausted. Shorter files are padded with empty fields.

Serial Pasting (-s option):
When invoked as `paste -s file1 file2`, it combines all lines of `file1` onto a single output line. Then, if `file2` is specified, it combines all lines of `file2` onto a *separate* output line, and so on. Each file's content is joined into its own distinct output line.

DELIMITER CYCLING

When using the -d LIST option, paste cycles through the characters provided in LIST as delimiters. For example, `paste -d ',-_' file1 file2 file3 file4` would use `,` between `file1` and `file2`, `-` between `file2` and `file3`, `_` between `file3` and `file4`, and then cycle back to `,` if there were more files.

HISTORY

paste is a fundamental Unix utility that has been part of the operating system since its early versions. It is standardized by POSIX, ensuring its consistent behavior across various Unix-like systems. Its design reflects the Unix philosophy of providing simple, powerful tools that can be combined to perform complex text processing tasks.

SEE ALSO

cat(1), cut(1), join(1), pr(1), awk(1)

Copied to clipboard