← Full reference

paste

Merge files line by line into columns, or join all lines of a file into one with a delimiter

paste merges files "side by side": the first line of the first file + the first line of the second, then the second lines, and so on. With -s it works differently — it joins all lines of one file into a single line. It's used to turn a column into a comma-separated list, add up numbers (paste -sd+) and lay out a stream in columns.

Syntax

paste [options] [file…]
paste -s -d, file               # all lines into one, comma-separated
command | paste - -             # two input lines into one

With no files or with - it reads standard input. Each - takes the next line from standard input, so paste - - - lays out a stream three values per line.

Options

OptionWhat it does
-d LISTdelimiters instead of tab. Each character of the list is used in turn, cyclically
-s"serial": each file as a whole becomes one line
-zlines are separated by a NUL byte, not \n

Special characters in -d

NotationMeaning
\ttab (the default)
\nnewline
\\backslash
\0empty delimiter: join with no gap (same as -d '')

Put the list in single quotes so the backslashes reach paste instead of being processed by the shell.

Examples

printf 'a\nb\nc\n' > letters.txt
printf '1\n2\n' > numbers.txt

# two tab-separated columns
paste letters.txt numbers.txt

# comma-separated — you get CSV
paste -d, letters.txt numbers.txt

# all lines into one
paste -s letters.txt              # tab-separated
paste -sd, letters.txt            # a,b,c

# sum of a column of numbers (no bc in the sandbox — use bash arithmetic)
echo $(( $(seq 5 | paste -sd+) ))

# join a stream two / three lines at a time
seq 6 | paste - -
seq 6 | paste -d, - - -

# "key: value" pairs with different delimiters in turn
printf 'host\nport\n' > keys.txt
printf 'db\n5432\n' > vals.txt
paste -d: keys.txt vals.txt

# cyclically: comma, then newline — comma-separated pairs
seq 6 | paste -sd ',\n'

# join with no delimiter
paste -d '' letters.txt numbers.txt

# standard input as the second column
paste letters.txt - < numbers.txt

Pitfalls

  • -d is a list of single characters, not a string. paste -d ', ' a b c puts a comma between the first and second column and a space between the second and third. A multi-character delimiter (, ) can't be done this way: join with -d, and then replace with sed, or build the line in awk.
  • Files of different length are not an error. Missing lines count as empty: c from the longer file gets an empty second column (c\t or c,). Check lengths with wc -l if it matters.
  • \r from Windows files "hides" inside the line: paste joins x\r and a, and the output looks garbled in the terminal. Remove \r beforehand (tr -d '\r').
  • paste -sd+ | bc is a common recipe, but bc isn't installed in the sandbox. Use $(( … )) for integers or awk '{s+=$1} END {print s}' for decimals.
  • -s with several files gives one line per file, not one combined line.
  • The last -d character isn't repeated at the end of the line with -s: paste -sd, on a b c gives a,b,c with no trailing comma.

See also

cut · join · column · tr · awk · seq