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
| Option | What it does |
|---|---|
-d LIST | delimiters instead of tab. Each character of the list is used in turn, cyclically |
-s | "serial": each file as a whole becomes one line |
-z | lines are separated by a NUL byte, not \n |
Special characters in -d
| Notation | Meaning |
|---|---|
\t | tab (the default) |
\n | newline |
\\ | backslash |
\0 | empty 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
-dis a list of single characters, not a string.paste -d ', ' a b cputs 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:
cfrom the longer file gets an empty second column (c\torc,). Check lengths withwc -lif it matters. \rfrom Windows files "hides" inside the line:pastejoinsx\randa, and the output looks garbled in the terminal. Remove\rbeforehand (tr -d '\r').paste -sd+ | bcis a common recipe, butbcisn't installed in the sandbox. Use$(( … ))for integers orawk '{s+=$1} END {print s}'for decimals.-swith several files gives one line per file, not one combined line.- The last
-dcharacter isn't repeated at the end of the line with-s:paste -sd,ona b cgivesa,b,cwith no trailing comma.