When working with text files and data streams in the Linux terminal, standard concatenation tools like cat append contents vertically from top to bottom. However, when you need to combine two or more files horizontally line by line—such as merging a list of IP addresses with corresponding domain names—the GNU core utility paste is the dedicated tool for the job.
Unlike join, which requires a common relational key field and sorted inputs, paste joins lines strictly based on their sequential position. It is lightweight, installed by default across virtually all Linux distributions, and exceptionally fast when handling massive datasets.
This guide explains how to use the paste command with practical examples, custom delimiters, serial transposition, and standard input piping.
Basic Syntax and Understanding How paste Works
The standard syntax for the paste command is straightforward:
paste [options] [file...]
By default, paste reads corresponding lines from each specified file and writes them to standard output separated by a Tab character (\t).
Example: Merging Two Files Side by Side
Suppose you have two simple text files:
users.txt
alice
bob
charlie
departments.txt
Engineering
Design
Operations
To join them side by side into a two-column layout, run:
paste users.txt departments.txt
Terminal Output:
alice Engineering
bob Design
charlie Operations
Customising the Delimiter with the -d Flag
By default, columns are delimited by tabs. You can substitute this with any character—such as a comma, semicolon, space, or colon—using the -d (delimiter) option.
1. Create CSV Output (Comma Separated)
To produce a clean comma-separated values (CSV) export:
paste -d, users.txt departments.txt > team_roster.csv
Output:
alice,Engineering
bob,Design
charlie,Operations
2. Merge with Colons or Custom Characters
For configuration files or connection records:
paste -d':' hosts.txt ports.txt
Output:
192.168.1.10:8080
192.168.1.11:443
192.168.1.12:22
3. Using Multiple Rotating Delimiters
If you specify multiple characters after -d, paste cycles through them sequentially across columns:
paste -d',=' names.txt roles.txt salaries.txt
This separates column 1 and 2 with a comma, and column 2 and 3 with an equals sign.
Transposing Lines Horizontally Using the -s (Serial) Flag
The -s (serial) flag completely alters how paste operates. Instead of reading one line from each file in parallel, it reads all lines of a single file and concatenates them into one horizontal row.
Consider users.txt:
paste -s users.txt
Output:
alice bob charlie
To convert a vertical column of values into a single comma-separated list (ideal for SQL IN (...) clauses or shell array definitions):
paste -s -d, users.txt
Output:
alice,bob,charlie
Advanced Techniques: Merging Consecutive Lines from Standard Input
One of the most powerful features of paste is its ability to read from standard input (represented by a hyphen -). When you pass multiple hyphens, paste consumes items sequentially from the input stream to construct multi-column tables.
Convert Single-Column Lists into Multi-Column Pairs
Suppose you have a file named raw_data.txt where every pair of lines represents a key and a value:
Hostname
web-server-01
IP_Address
10.0.0.15
Status
Active
To merge every two consecutive lines into a structured key-value table:
paste - - < raw_data.txt
Output:
Hostname web-server-01
IP_Address 10.0.0.15
Status Active
To format this output as a colon-separated list:
paste -d': ' - - < raw_data.txt
Comparing Linux Text Joining Tools
To choose the right utility for text manipulation, refer to the following comparison:
| Command | Operation | Key Requirement | Typical Use Case |
|---|---|---|---|
cat |
Vertical concatenation | None | Merging logs or appending files end-to-end. |
paste |
Horizontal joining by line index | None | Combining column files side by side or serialising rows. |
join |
Relational database-style join | Identical sorted key field | Combining records sharing a common ID. |
pr |
Paginated and multi-column formatting | None | Preparing formatted output for printing or terminal display. |
Troubleshooting Common paste Gotchas
Unequal File Lengths
When input files have differing numbers of lines, paste does not stop early or throw an error. It outputs empty fields (delimiters without values) for the shorter files until the longest file is exhausted. If your data requires exact row matching, verify line counts first using wc -l file1 file2.
Mismatched Output Due to Windows CRLF Line Endings
If output appears scrambled or overwrites previous text, your files may contain Windows-style carriage returns (\r\n). A trailing \r causes the terminal cursor to return to the beginning of the line before writing the next column. Strip carriage returns before pasting:
dos2unix file1.txt file2.txt
Alternatively, strip them on the fly using tr:
tr -d '\r' < file1.txt | paste - file2.txt