paste — Merge Lines Side by Side
Practical guide to paste: merge lines from several files side by side into columns, use custom delimiters and build CSV data.
paste merges lines from several files side by side into columns – separated by a tab by default, or by any delimiter you like (-d). With it you assemble tables or CSV data from individual column files in no time. With -s you flip things around and instead join all lines of a single file into one line. Via the - placeholder paste also reads from standard input, so you can reflow a flat data stream into multiple columns. This guide shows you the most important variants and their pipeline tricks.
Basic Usage
paste <file1> <file2> — Merge lines side by side, separated by tab.
paste names.txt scores.txtpaste -d '<delim>' <file1> <file2> — Use a custom delimiter instead of tab.
paste -d ',' names.txt scores.txtpaste -d '\n' <file1> <file2> — Interleave lines from two files (alternate lines).
paste -d '\n' questions.txt answers.txtpaste -s <file> — Join all lines of a file into one line (serial mode).
paste -s names.txtpaste -s -d ',' <file> — Join all lines with a comma (create CSV-like output).
paste -s -d ',' names.txtUsing Standard Input
cat <file> | paste - - — Arrange input into 2 columns.
seq 6 | paste - -cat <file> | paste - - - — Arrange input into 3 columns.
seq 9 | paste - - -cat <file> | paste -d ',' - - — Arrange into 2 comma-separated columns.
seq 6 | paste -d ',' - -paste <file> - — Merge a file with stdin side by side.
seq 3 | paste names.txt -Multiple Delimiters
paste -d ',;:' <file1> <file2> <file3> <file4> — Cycle through multiple delimiters for each column separator.
paste -d ',;:' f1.txt f2.txt f3.txt f4.txtpaste -d ',\n' - - — Alternate between comma and newline as delimiters.
seq 6 | paste -d ',\n' - -paste -d '\t|' <file1> <file2> <file3> — Use tab then pipe as alternating delimiters.
paste -d '\t|' col1.txt col2.txt col3.txtCommon Patterns
ls | paste -s -d ' ' — List files on a single line, space-separated.
ls *.txt | paste -s -d ' 'cut -f1 data.tsv | paste -s -d '+' — Create a sum expression from a column of numbers.
cut -f1 data.tsv | paste -s -d '+' | bcpaste -d ',' <(cmd1) <(cmd2) — Combine output of two commands side by side.
paste -d ',' <(cut -f1 data.tsv) <(cut -f3 data.tsv)seq <n> | paste -s -d ',' — Generate a comma-separated sequence of numbers.
seq 10 | paste -s -d ','awk '{print $1}' file | paste -s -d '|' — Create a pipe-delimited list from a column.
awk '{print $1}' users.txt | paste -s -d '|' Conclusion
paste is the counterpart to cut: where cut splits columns out, paste joins them back together. Keep in mind that paste works purely by line position – it joins the n-th line of each file with no key matching whatsoever; for columns linked by content you need join instead. With -d you provide several delimiters as a list that is cycled through for the column transitions. Write control characters such as tab or newline as '\t' or '\n' in single quotes so the shell passes them through to paste unchanged.
Further Reading
- GNU Coreutils manual: paste – complete reference for every option
- man7.org: paste(1) – the Linux manual page