Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

paste Command Guide for Merging Lines

This guide helps you understand and use the paste command, the standard way to merge lines from files side by side.


What is paste, and Why Use It?

The paste command merges corresponding lines of two or more files side by side, separated by a delimiter. While cut slices a single line into pieces, paste does the opposite: it stitches lines together horizontally. Its name comes from “paste” — like pasting columns together in a spreadsheet.

Why Learn paste?

  • Building lists from columns: Turn two lists (names and scores) into a joined table with one command.
  • CSV creation: Joining tab-separated files into comma-separated ones is a one-liner.
  • Serial numbers and counters: paste can generate sequences to number lists.
  • It’s the mirror of cut: Learning both gives you the full picture of columnar text manipulation.

paste works on lines of files, not on computed values. If you need logic or arithmetic, use awk.


1. Paste Two Files Side by Side

With no options, paste merges lines with a tab between them:

paste names.txt scores.txt

If names.txt is:

alice
bob
carol

and scores.txt is:

95
87
92

the output is:

alice	95
bob	87
carol	92

Files can be given as arguments, or piped in with - for stdin:

cat names.txt | paste - scores.txt

2. Change the Delimiter (-d)

-d replaces the tab with any delimiter you choose — the key to building CSVs:

paste -d, names.txt scores.txt

Output:

alice,95
bob,87
carol,92

Multiple delimiters rotate across the fields:

paste -d,;- names.txt scores.txt
  • First merge uses ,, the next uses ;, then - (no delimiter) again.

Join three files:

paste -d, a.txt b.txt c.txt

3. Merge Lines of One File (-s)

-s (serial) pastes the lines of each file into one long line instead of side by side:

paste -s -d, names.txt

Output (one line):

alice,bob,carol

This is the quickest way to flatten a list into a comma-separated string:

printf '%s\n' apple banana cherry | paste -s -d' '

Output:

apple banana cherry

4. Pair Up Lines from One Stream (-)

Each - argument reads stdin once more, so paste - - takes two lines at a time and puts them on one row:

ls | paste -d' ' - -
  • Lines are paired: row 1 gets lines 1–2, row 2 gets lines 3–4, and so on.
  • This is the classic way to make a two-column layout from a single stream.

A traditional numbered-list trick combines cat -n with paste:

cat -n names.txt | paste - -
  • cat -n prefixes each line with its number; paste - - merges the numbered line with the next, producing “number name” rows.

To flatten a numbered list into a single comma-separated line:

seq 10 | paste -s -d,

Output:

1,2,3,4,5,6,7,8,9,10

5. Build a CSV from a Delimited File

A very common real-world task: convert colon-separated /etc/passwd output into a proper CSV:

cut -d: -f1,3,6 /etc/passwd | paste -d, - - -

Or more directly, since paste can re-delimit an existing file when combined with tr:

tr ':' ',' < /etc/passwd

(See tr for that variant — paste is the tool when you’re joining separate streams.)


6. Interleave Lines from Multiple Streams

paste reads all inputs in parallel, line by line. This makes it perfect for interleaving:

paste -d' ' <(echo first) <(echo second)

Output:

first second

A practical example — join a list of hosts with a list of usernames to build an SSH command list:

paste -d' ' hosts.txt users.txt | awk '{print "ssh " $2 "@" $1}'

Common Pitfalls

  • Default delimiter is a tab. Output that looks “weirdly spaced” in a terminal is usually tabs, not spaces.
  • Missing lines are padded, not skipped. If one file is shorter, its lines are blank for the extra rows — use --delimiters care or check line counts with wc first.
  • paste doesn’t sort. It merges by line position, so misaligned files produce garbage. Sort first with sort if order matters.
  • - is stdin, and each - consumes one read. Multiple - arguments read from stdin multiple times, which is easy to misuse.

PageRelationship
cutThe mirror operation: cut splits columns out, paste merges columns in.
trReplaces delimiters in a single stream; complements paste’s -d.
sortEnsures inputs are aligned before pasting.
awkJoins files by key (like a database join), which paste cannot do.
wcVerify line counts match before merging.

paste Cheatsheet

FlagMeaning
(no flag)Join files side by side with a tab delimiter.
-d LISTDelimiters to use; they rotate across inputs.
-sSerial: join lines of each file into one line.
-Read from stdin (one read per -).
--delimiters LISTLong form of -d.

Next: write output to a file and the screen at once — tee.