VMware ESXi and vSphere Cluster Management
Merge Files Line by Line in Linux with the paste Command
Learn how to merge corresponding lines from Linux text files with paste, choose delimiters, use serial mode, handle unequal files, and save the result.
The Linux paste command combines corresponding lines from two or more text files. It places the content side by side, creating one output row for each set of input lines.
This is called a horizontal merge: content from separate files appears on the same line. It differs from vertical concatenation, where one file is appended after another, as with cat.
Horizontal Merging Versus Vertical Concatenation
For example, if one file contains names and another contains colors, a horizontal merge can create name-color pairs. A vertical concatenation instead produces all names followed by all colors.
Basic paste Usage
The basic syntax is:
paste file_1 file_2
In the default parallel mode, paste reads one physical line from each input file for every output row:
# file_1
apple
banana
cherry
# file_2
red
yellow
red
$ paste file_1 file_2
apple red
banana yellow
cherry red
The spaces shown above represent a tab character, not necessarily several space characters. Line 1 from each file becomes output line 1, line 2 becomes output line 2, and so on.
You can provide more than two files:
paste file_1 file_2 file_3
Each output row then contains the same-numbered line from all three files.
Default Tab Delimiter
A delimiter is a character placed between fields in output. By default, paste uses a tab character, written conceptually as \t.
Tabs are useful for column-oriented data, but terminal display can look uneven. A tab advances to the next tab stop, so fields with different widths may make later columns appear misaligned. This does not necessarily mean that the data is wrong.
To inspect the output, you can make tabs visible with tools such as cat -T:
paste file_1 file_2 | cat -T
You can also use a deliberately visible delimiter while testing:
paste -d '/' file_1 file_2
When passing the result to another tool, account for the actual delimiter. A tab-delimited file should be parsed as tab-separated data, not as data separated by ordinary spaces. Existing tabs inside input fields can also create additional fields for later parsers.
Choosing a Delimiter with -d
Use -d to select the output delimiter:
paste -d '/' file_1 file_2
apple/red
banana/yellow
cherry/red
Other common choices include a comma, colon, or space:
paste -d ',' file_1 file_2
paste -d ':' usernames.txt user_ids.txt
paste -d ' ' file_1 file_2
Choose a delimiter that matches the intended downstream format. A comma may suit simple comma-separated data, a colon is common for uncomplicated configuration-like records, and a slash may make interactive output easy to read.
Delimiter Lists
The argument to -d can contain a list of delimiters. When several files are merged, paste uses the delimiters in sequence and reuses them when necessary.
paste -d ',:/' names.txt cities.txt countries.txt
For each output row, the first separator is used between the first and second fields, the next separator between the second and third fields, and then the delimiter sequence is reused for additional fields. A single delimiter such as , is reused between every field.
Serial Output with -s
The -s option enables serial mode. Instead of reading one line from every file for each output row, paste reads all lines from the first file and places them on one line, then does the same for the next file.
# file_1
apple
banana
cherry
# file_2
red
yellow
red
$ paste -s file_1 file_2
apple banana cherry
red yellow red
Serial mode is useful when each input file should become one output record:
paste -s -d ',' file_1
apple,banana,cherry
Here, -s joins the lines from one file, while -d ',' selects the comma between values. Without -s, the command would merge corresponding lines from separate inputs in parallel.
Saving and Using Merged Output
paste writes its result to standard output, the command output stream normally displayed in the terminal. Use shell output redirection with > to save it:
paste -d ':' usernames.txt user_ids.txt > users.txt
This creates or overwrites users.txt while leaving the two input files unchanged.
Do not redirect output to an input file that paste still needs to read:
# Unsafe: the shell can truncate file_1 before paste reads it
paste file_1 file_2 > file_1
The shell opens the redirection target before starting the command. Opening it for writing can empty the file first, producing incomplete or empty input. Write to a separate temporary or final file, inspect it, and replace an original only afterward if that is really intended.
Using Standard Input
A hyphen, -, represents standard input as one of the inputs. This allows a pipeline to provide one side of the merge:
printf '%s\n' one two | paste -d ':' - file_2
The lines produced by printf are read as the first input, while file_2 supplies the second input.
paste Options and Effects
Unequal Line Counts and Input Shape
paste works on physical lines. It does not identify records by an ID, compare content, or search for matching keys. The first line of one file is paired with the first line of the other, regardless of what those lines contain.
For example, if a two-line file is merged with a three-line file, the third output row contains an empty field for the shorter input:
# short.txt
A
B
# long.txt
1
2
3
$ paste -d ':' short.txt long.txt
A:1
B:2
:3
Before merging data that is expected to represent matching records, compare line counts:
wc -l file_1 file_2
Equal line counts do not prove that records correspond correctly; they only confirm that the files have the same number of physical lines.
Practical Patterns
Merge Several Related Columns
paste -d ',' names.txt cities.txt countries.txt
This creates one row for each line position across the three files. The comma is reused between each field.
Turn Newline-Separated Values into One Row
paste -s -d ',' file_1
This is useful for a simple list, but it is not a complete CSV writer. Values containing commas, quotes, or newlines need CSV-aware handling.
Inspect a Merge Before Saving It
paste -d ':' usernames.txt user_ids.txt
paste -d ':' usernames.txt user_ids.txt > users.txt
Run the command without redirection first, validate the rows and delimiter, then save the result.
Troubleshooting
Columns Do Not Line Up in the Terminal
The default separator is a tab, and preceding values may have different display widths. Use a visible custom delimiter for inspection, display tabs explicitly, or use a formatting tool such as column when presentation matters.
Blank Values Appear Near the End
One input probably has fewer lines. Use wc -l to compare counts and confirm that each physical line represents the intended record.
The Output Is One Long Row per File
The -s option enabled serial mode. Remove -s to return to normal parallel, line-by-line merging.
A Comma-Separated Result Is Hard to Parse
Input values may already contain commas. paste does not perform CSV quoting or escaping. Use a delimiter that cannot occur in the data, sanitize the inputs, or use a CSV-aware tool when valid CSV behavior is required.
An Input File Became Empty
The output was likely redirected to one of the files being read. Write to a different output filename, validate the result, and only then replace an original file.
Exam-Relevant Notes
- Parallel mode is the default: one line from each input becomes one output row.
- Serial mode uses
-s: each file is processed as a separate sequence of values on one row. - The default delimiter is a tab: it is not a group of ordinary spaces.
-dselects delimiters: a delimiter list is used in sequence and reused as needed.pastematches line positions, not keys: use a key-based tool such asjoinwhen records must match by a field.- Standard output is not automatically saved: use
>to write a file. - Never overwrite an input during the same read operation: redirection can truncate it before the command starts.
Summary
Use paste file_1 file_2 for a horizontal, line-by-line merge with tab separators. Add -d when the output needs a chosen delimiter, and add -s when each file should be joined serially. Check line counts and record order before merging, remember that the command works by physical line position, and redirect output only to a separate destination.
For related operations, see this guide to merging files line by line.