Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Text Processing

Linux paste command

Reference for the Linux paste command: join files side by side, merge lines of one file into rows, choose delimiters with -d, serial mode with -s and stdin tricks.

paste joins corresponding lines of several files, separated by a tab. The first line of every file goes on the first output line, the second lines on the second, and so on. It is the horizontal counterpart of cat: cat stacks files vertically, paste puts them next to each other.

With the serial option -s it does the opposite job and merges all lines of one file into a single line, which makes it handy for turning a column into a comma-separated list. Combined with - as a file name it can also fold a stream into rows of a fixed width.

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

Try it

Bash Side by side and serial

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Rows of N from a stream and delimiter lists

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
paste [-s] [-d LIST] [-z] [FILE]...

A file name of - reads standard input. Repeating - takes successive lines from the same stream.

Option Description
-d LIST Use the characters of LIST as delimiters, one after another, wrapping around. Default is a tab.
-s Serial: paste each file's lines into one line, one output line per file.
-z Lines end with NUL.

Escape sequences in the delimiter list: \t tab, \n newline, \\ backslash, \0 empty (no delimiter).

Returns: 0 on success, 1 on an error such as an unreadable file.

Usage notes

  • Shorter files run out first; paste then prints empty fields for them, so the delimiters still line up.
  • -d is a list, not a string. -d', ' does not insert a comma followed by a space; it alternates comma and space between columns. For a multi-character separator, post-process with sed.
  • Use \0 for no separator: paste -sd '\0' file removes the newlines and glues the lines together.
  • Column to list: paste -sd, file produces a,b,c. There is no trailing delimiter, unlike with tr '\n' ','.
  • Rows of N: use - N times, as in paste - - -. An incomplete last row is padded with empty fields.
  • Not a join. paste pairs lines by position only. To pair lines by a common key use join.
  • Pairs well with process substitution, such as paste <(cut -f1 a) <(cut -f3 b) to build a table from columns of different files.
  • Windows line endings end up in the middle of the output line (the carriage return stays on the first file's field). Convert with tr -d '\r' first.