GICT knowledge base
How to Use the paste Command to Merge File Lines Side by Side in Linux
Published
Text file manipulation is a fundamental part of working in the Linux command line. Sometimes, you need to combine the contents of different files—such as merging data columns, preparing reports, or aligning outputs from separate sources. The paste command is a powerful but often overlooked tool that lets you merge the lines of multiple files side by side, producing output that’s efficient for both quick inspections and downstream processing.
This article introduces the paste command, explains its options, and provides practical examples for real-world tasks. By the end, you’ll be able to merge, format, and manipulate file lines confidently with paste.
Prerequisites
Before you begin, make sure you have:
- Access to a Linux machine or terminal
- Two or more text files to experiment with (you can create sample files as needed)
- Basic familiarity with command-line navigation
Introduction to the paste Command
The paste command merges lines from multiple input files, placing their contents side by side. By default, each line is separated by a tab, but you can customize the delimiter. paste is especially useful for:
- Merging columns from separate files
- Building simple tables
- Preparing data for further processing in spreadsheets or databases
The basic syntax is:
$ paste [OPTION]... [FILE]...
Creating Sample Files
Let’s create two sample files for demonstration.
File: names.txt
Alice
Bob
Charlie
David
Eve
File: scores.txt
85
92
78
90
88
Create these files with:
$ echo -e "Alice\nBob\nCharlie\nDavid\nEve" > names.txt
$ echo -e "85\n92\n78\n90\n88" > scores.txt
Basic Usage of paste
Step 1: List All Open Files Side by Side
$ paste names.txt scores.txt
Output:
Alice 85
Bob 92
Charlie 78
David 90
Eve 88
Each line from names.txt is paired with the corresponding line from scores.txt, separated by a tab.
Step 2: Merging More Than Two Files
You can merge more than two files at once. Suppose you have a third file, grades.txt:
$ echo -e "A\nA+\nB\nA\nB+" > grades.txt
$ paste names.txt scores.txt grades.txt
Output:
Alice 85 A
Bob 92 A+
Charlie 78 B
David 90 A
Eve 88 B+
Step 3: Customizing the Delimiter
By default, paste uses a tab to separate columns. Use the -d option to specify a custom delimiter.
Use a comma:
$ paste -d ',' names.txt scores.txt
Alice,85
Bob,92
Charlie,78
David,90
Eve,88
Use a custom string:
$ paste -d ';|' names.txt scores.txt grades.txt
Alice;85|A
Bob;92|A+
Charlie;78|B
David;90|A
Eve;88|B+
The first delimiter (;) separates the first and second file, the second delimiter (|) separates the second and third. If you have more columns than delimiters, the delimiters cycle.
Step 4: Handling Files of Different Lengths
If one file has more lines than the other(s), paste fills missing fields with empty strings.
Edit scores.txt to add a new line (100) at the end:
85
92
78
90
88
100
Now run:
$ paste names.txt scores.txt
Alice 85
Bob 92
Charlie 78
David 90
Eve 88
100
Notice the last line is blank for names.txt—paste always outputs a line for every line in the longest file.
Step 5: Merging Standard Input with Files
The dash (-) represents standard input. This is useful for combining piped or interactive input with file contents.
$ echo -e "Red\nBlue\nGreen\nYellow\nPurple" | paste - names.txt
Red Alice
Blue Bob
Green Charlie
Yellow David
Purple Eve
Step 6: Using Serial Mode (-s)
The default behavior of paste is to merge files column-wise. With the -s (serial) option, it merges lines from a file into a single line, then moves to the next file.
$ paste -s names.txt
Alice Bob Charlie David Eve
Use multiple files:
$ paste -s names.txt scores.txt
Alice Bob Charlie David Eve
85 92 78 90 88
Change delimiter with -d:
$ paste -s -d ',' names.txt
Alice,Bob,Charlie,David,Eve
Combining paste with Other Commands
Step 7: Combine Specific Columns from a CSV
$ cut -d ',' -f1 file.csv > col1.txt
$ cut -d ',' -f3 file.csv > col3.txt
$ paste col1.txt col3.txt
Step 8: Add Line Numbers to a File
$ nl -n ln names.txt | cut -f1 > numbers.txt
$ paste numbers.txt names.txt
Output:
1 Alice
2 Bob
3 Charlie
4 David
5 Eve
Step 9: Pair Output from Two Different Commands
$ ls /etc | paste - <(ls /usr/bin)
Pairs each file from /etc with one from /usr/bin.
Step 10: Formatting Output with column
The tab-separated output from paste may not always look aligned in terminals with variable-width fonts. For a neat, table-like display, pipe the output into the column command:
$ paste names.txt scores.txt | column -t
Alice 85
Bob 92
Charlie 78
David 90
Eve 88
Real-World Use Cases
Step 11: Combine Usernames and Passwords
$ paste users.txt passwords.txt
Quickly generates pairs for testing or batch processing.
Step 12: Merge Date and Log Files
$ paste dates.txt logs.txt > merged_report.txt
Step 13: Generate Simple CSV Files
$ paste -d ',' column1.txt column2.txt > file.csv
Step 14: Prepare Data for Databases
Format columns for import into MySQL or SQLite, using custom delimiters or serial mode as needed.
Common Issues and Troubleshooting
Step 15: Output Columns Not Aligned
If columns appear jagged, pipe the output to column -t for alignment.
Step 16: Files Have Different Numbers of Lines
The output will always have as many lines as the longest file. Missing fields are blank. Double-check your files to avoid accidental misalignment.
Step 17: Custom Delimiters Not Working
If your delimiter is a special character (like a space or backslash), enclose it in quotes:
$ paste -d " " file1.txt file2.txt
Step 18: Merging with Empty or Large Files
If one file is empty, the output will be blank in that column. With very large files, use head or tail to preview output before merging.
Scripting with paste
Step 19: Automate Merging Daily Data Columns
#!/bin/bash
for file in data/*.col1; do
base=$(basename "$file" .col1)
paste "data/${base}.col1" "data/${base}.col2" > "data/${base}_combined.txt"
done
Summary Table: Key paste Options
| Option | Description | Example |
|---|---|---|
| -d DELIM | Use DELIM as the column separator | paste -d ‘,’ file1 file2 |
| -s | Serial mode: merge lines from each file in series | paste -s file1 |
| – | Use standard input as a file | paste – file1 |
Best Practices
- Preview your files with cat or head before merging.
- Use meaningful delimiters that don’t conflict with your data.
- Pipe output to column -t for readable, table-like formatting.
- Check for equal line counts to avoid empty entries.
- Script repetitive merges for efficiency and consistency.
- Combine with awk, cut, or sort for advanced data prep.
Conclusion
The paste command is a versatile and efficient tool for merging lines from multiple files side by side in Linux. Whether you are preparing datasets, generating reports, or simply combining lists for quick analysis, paste lets you align and merge data with ease. By mastering its options and combining it with other command-line utilities, you can automate and streamline your text-processing workflows.
Practice using paste for different scenarios—experiment with delimiters, serial mode, and command combinations—to fully leverage its power in your Linux toolbox.
Related guides
How to Set Up zram on a Small VPS
Configure zram on Ubuntu with kernel and swap checks. Understand compressed memory limits, monitor real use and avoid unsafe swap-file changes.
LinuxHow to Find Services That Need a Restart After Updates with needrestart
Find services using outdated libraries with needrestart on Ubuntu. Read restart reports, review automatic restart policy and plan maintenance safely.
LinuxHow to Limit a Linux Service’s Memory with systemd
Limit a Linux service with systemd MemoryHigh, MemoryMax and swap controls. Check cgroup support, diagnose pressure and remove only your own policy.
LinuxHow to Protect SSH with CrowdSec on Ubuntu
Protect SSH with CrowdSec on Ubuntu. Connect log detection to firewall enforcement and test temporary blocking without losing administrator access.
