# 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.

Source: https://www.jpkc.com/db/en/cheatsheets/files-text/paste/

<!-- PROSE:intro -->
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.
<!-- PROSE:intro:end -->

## Basic Usage

`paste <file1> <file2>` — Merge lines side by side, separated by tab.

```bash
paste names.txt scores.txt
```

`paste -d '<delim>' <file1> <file2>` — Use a custom delimiter instead of tab.

```bash
paste -d ',' names.txt scores.txt
```

`paste -d '\n' <file1> <file2>` — Interleave lines from two files (alternate lines).

```bash
paste -d '\n' questions.txt answers.txt
```

`paste -s <file>` — Join all lines of a file into one line (serial mode).

```bash
paste -s names.txt
```

`paste -s -d ',' <file>` — Join all lines with a comma (create CSV-like output).

```bash
paste -s -d ',' names.txt
```

## Using Standard Input

`cat <file> | paste - -` — Arrange input into 2 columns.

```bash
seq 6 | paste - -
```

`cat <file> | paste - - -` — Arrange input into 3 columns.

```bash
seq 9 | paste - - -
```

`cat <file> | paste -d ',' - -` — Arrange into 2 comma-separated columns.

```bash
seq 6 | paste -d ',' - -
```

`paste <file> -` — Merge a file with stdin side by side.

```bash
seq 3 | paste names.txt -
```

## Multiple Delimiters

`paste -d ',;:' <file1> <file2> <file3> <file4>` — Cycle through multiple delimiters for each column separator.

```bash
paste -d ',;:' f1.txt f2.txt f3.txt f4.txt
```

`paste -d ',\n' - -` — Alternate between comma and newline as delimiters.

```bash
seq 6 | paste -d ',\n' - -
```

`paste -d '\t|' <file1> <file2> <file3>` — Use tab then pipe as alternating delimiters.

```bash
paste -d '\t|' col1.txt col2.txt col3.txt
```

## Common Patterns

`ls | paste -s -d ' '` — List files on a single line, space-separated.

```bash
ls *.txt | paste -s -d ' '
```

`cut -f1 data.tsv | paste -s -d '+'` — Create a sum expression from a column of numbers.

```bash
cut -f1 data.tsv | paste -s -d '+' | bc
```

`paste -d ',' <(cmd1) <(cmd2)` — Combine output of two commands side by side.

```bash
paste -d ',' <(cut -f1 data.tsv) <(cut -f3 data.tsv)
```

`seq <n> | paste -s -d ','` — Generate a comma-separated sequence of numbers.

```bash
seq 10 | paste -s -d ','
```

`awk '{print $1}' file | paste -s -d '|'` — Create a pipe-delimited list from a column.

```bash
awk '{print $1}' users.txt | paste -s -d '|'
```

<!-- PROSE:outro -->
## 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](https://www.gnu.org/software/coreutils/manual/html_node/paste-invocation.html) – complete reference for every option
- [man7.org: paste(1)](https://man7.org/linux/man-pages/man1/paste.1.html) – the Linux manual page
<!-- PROSE:outro:end -->

## Related Commands

- [cut](https://www.jpkc.com/db/en/cheatsheets/files-text/cut/) – extract columns and fields from lines
- [column](https://www.jpkc.com/db/en/cheatsheets/files-text/column/) – format input into clean columns
- [tr](https://www.jpkc.com/db/en/cheatsheets/files-text/tr/) – translate or delete individual characters

