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.
paste [OPTION]... [FILE]...
Try it
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.
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
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;
pastethen prints empty fields for them, so the delimiters still line up. -dis 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 withsed.- Use
\0for no separator:paste -sd '\0' fileremoves the newlines and glues the lines together. - Column to list:
paste -sd, fileproducesa,b,c. There is no trailing delimiter, unlike withtr '\n' ','. - Rows of N: use
-N times, as inpaste - - -. An incomplete last row is padded with empty fields. - Not a join.
pastepairs lines by position only. To pair lines by a common key usejoin. - 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.