paste
Merge corresponding lines of files side by side into columns
By CMD Script Team · 4 min read · Last updated
paste [OPTIONS] FILE...Options
| Flag | Description |
|---|---|
-d LIST | Use the given delimiter(s) instead of the default tab between merged fields |
-s | Serial mode: merge all lines of each file into a single line, instead of merging across files |
- | Read from standard input in place of a filename, useful in pipelines |
--zero-terminated | Treat input as NUL-terminated lines instead of newline-terminated |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS
What it does
paste merges corresponding lines from two or more files horizontally: line 1 of each
file becomes one output line (joined by tabs by default), line 2 of each file becomes the
next output line, and so on. It's essentially the transpose of cat, which stacks files
vertically — paste lines them up side by side as columns, which is useful for quickly
assembling simple tabular data from separate single-column files.
Beginner examples
paste names.txt scores.txt— combine two files into two tab-separated columnspaste -d, file1.txt file2.txt— use a comma instead of a tab as the delimiterpaste file1.txt file2.txt file3.txt— merge three files into three columnspaste -s file.txt— join every line of a single file into one line, tab-separated
paste usernames.txt emails.txt
Advanced examples
- Build a quick CSV from two single-column files:
paste -d, names.txt scores.txt > people.csv - Flatten a column of values into a single comma-separated row:
paste -s -d, ids.txt - Combine
pastewith process substitution to merge command output without temp files:paste <(cut -d, -f1 data.csv) <(cut -d, -f3 data.csv) - Use multiple delimiters that cycle across columns:
paste -d ':,' file1.txt file2.txt file3.txt(colon between first pair, comma between next, then repeats) - Merge sorted key-value files where positions align exactly (when a proper
joinisn't needed):paste keys.txt values.txt
paste <(cut -d, -f1 data.csv) <(cut -d, -f3 data.csv) > subset.csv
Common mistakes
- Using
pastewhen the files aren't line-aligned — since it merges purely by line position, mismatched files produce nonsensical rows with no warning. - Reaching for
pastewhen you actually need to merge on a matching key field, which isjoin's job, notpaste's —pastehas no concept of matching values, only line position. - Forgetting the default delimiter is a tab, which can look like several spaces in a terminal and cause confusion when the output is later parsed as comma-separated.
- Using
-swhen you meant the default column-merging mode (or vice versa) — they produce very different shaped output.
Tips
- Use process substitution (
<(...)) topastetogether columns extracted on the fly from a single source file, without creating temporary files. paste -s -d, fileis a quick way to turn a newline-separated list into a single comma-separated line, handy for building shell arguments or simple reports.- Check that all input files have the same number of lines before pasting, since a shorter file simply leaves empty fields for its missing lines rather than raising an error.
Best practices
- Use
pastefor simple, positionally-aligned column assembly; usejoininstead as soon as you need to match rows by a key rather than by line number, since mismatched ordering withpastesilently produces wrong data. - Sort and verify line counts of input files before pasting when building anything more than a throwaway one-off, to avoid silent misalignment.
- Prefer
-dwith an explicit delimiter over relying on the default tab when the output will be consumed by another tool expecting a specific format like CSV.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Quickly building a two-column report from separately generated lists, such as usernames and their corresponding email addresses.
- Turning a column of IDs into a single comma-separated string to embed in a SQL
IN (...)clause:paste -s -d, ids.txt. - Reassembling extracted columns from a larger CSV after processing them independently
with
cut.
Common interview questions
- What does the paste command do? It merges corresponding lines from two or more files side by side into columns, joined by a delimiter (tab by default).
- What's the difference between paste and join?
pastemerges purely by line position with no regard for content;joinmerges rows based on matching a common key field, similar to a database join, and requires sorted input on that key. - How would you turn a file's lines into a single comma-separated line?
paste -s -d, file.txt, which uses serial mode to merge all lines of the file into one, separated by commas.
Frequently Asked Questions
What does paste actually do?
paste reads corresponding lines from two or more files and joins them side by side on one output line, separated by a delimiter (tab by default), effectively turning several files into columns of a combined table.
How do I change the delimiter between merged columns?
Use -d, e.g. paste -d, file1.txt file2.txt joins lines with a comma instead of the default tab, which is handy for quickly assembling a simple CSV.
What does paste -s do?
-s (serial) transposes the operation: instead of merging across files line by line, it merges all the lines within a single file into one line, joined by the delimiter. This is useful for turning a column of values into a single comma- or space-separated row.
How is paste different from join?
paste merges files purely positionally — line 1 with line 1, line 2 with line 2, regardless of content. join merges based on matching a common key field in both files, similar to a database join, and requires the files to be sorted on that key.
Cheat sheet
Download a quick-reference cheat sheet for paste.