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 [[-s|--serial]] [[-d|--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

-s, --serial
    Paste one file at a time instead of in parallel.

-d, --delimiters=LIST
    Reuse characters from LIST instead of TABs.

-z, --zero-terminated
    Line delimiter is NUL, not newline.

--help
    Display help and exit.

--version
    Output version information and exit.

DESCRIPTION

The paste command is a utility in Linux and Unix-like operating systems used to merge corresponding lines of one or more files. It reads lines sequentially from each file, and combines them into a single line separated by delimiters (by default, a tab). The result is written to standard output.

It is commonly used to combine data from different sources based on line number. paste can be used to quickly create tabular data from columns that are stored in separate files. It is also capable of handling input from standard input, allowing it to be used in pipelines with other commands. If a file is not provided, or if a hyphen (-) is specified as a file, paste reads from standard input. This facilitates dynamic data processing within shell scripts and command-line workflows. It's a versatile tool for data manipulation, particularly useful when the order of lines is crucial.

CAVEATS

If the input files have different lengths, paste will continue to process the longest file, substituting empty strings for the missing lines in shorter files. This behavior may need to be considered when working with data that requires precise alignment. Also note that the number of files that can be pasted together might be limited by the system's maximum number of open files.

EXAMPLES

Paste two files side by side using a tab as the delimiter:
paste file1.txt file2.txt

Paste two files side by side using a comma as the delimiter:
paste -d, file1.txt file2.txt

Paste files serially using a newline character as the delimiter:
paste -s -d'\n' file1.txt

Read from standard input and paste with a file:
cat file1.txt | paste - file2.txt

HISTORY

paste has been a part of Unix systems for a long time, evolving as part of the standard toolset. It provides a basic but effective way of combining data from multiple streams and files, playing an important role in scripting and data manipulation workflows across various versions of Unix-like operating systems. Its consistent functionality ensures portability across different distributions.

SEE ALSO

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

Copied to clipboard