paste Command — Merging Files Horizontally in Linux¶
The
pastecommand is used to merge lines from multiple files horizontally. Unlike thecatcommand, which combines files vertically,pastejoins corresponding lines side by side using a delimiter. It is widely used for combining reports, CSV files, configuration data, and command outputs.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand the
pastecommand - Merge multiple files horizontally
- Change delimiters
- Merge command outputs
- Work with CSV-like data
- Combine
pastewith other Linux commands - Process real-world reports
Prerequisites¶
Before starting this lesson, complete:
- Module 1 – Linux Fundamentals
- Module 2 – Linux Command Line Essentials
- Module 3 Lessons 1–7
Why Learn paste?¶
Suppose you have two separate files.
names.txt
departments.txt
Instead of manually combining them:
Use:
Done instantly.
What is paste?¶
The paste command joins corresponding lines from multiple files into a single output.
Syntax:
By default, it separates columns with a tab.
Sample Files¶
Create:
Create:
Merge Two Files¶
Output:
(Default delimiter: Tab)
Merge Three Files¶
Create:
countries.txt
Run:
Output:
Change the Delimiter¶
Use commas.
Output:
Use a colon.
Output:
Use Multiple Delimiters¶
Output:
The delimiters are used in sequence.
Serial Mode¶
Normally:
Output:
Serial mode:
Output:
Comma-separated serial output.
Output:
Merge Command Output¶
Merge usernames and login shells.
Example:
Note
The <(...) syntax is called process substitution and is supported in Bash. We'll cover it in the Shell Scripting module.
Combining with Other Commands¶
Sort before merging.
Merge with line numbers.
Create a CSV.
Common paste Options¶
| Option | Description |
|---|---|
-d | Specify delimiter |
-s | Serial mode |
-- | End option processing |
Difference Between cat and paste¶
| Command | Behavior |
|---|---|
cat | Combines files vertically |
paste | Combines files horizontally |
Example:
cat
paste
Real Production Examples¶
Merge usernames and user IDs.
Generate CSV reports.
Merge monitoring reports.
Combine Kubernetes reports.
Create inventory files.
Production Perspective¶
The paste command is useful for:
- Creating CSV reports
- Combining inventory data
- Joining monitoring outputs
- Merging configuration values
- Building shell automation reports
It is commonly used together with cut, sort, and shell scripts.
Hands-on Lab¶
Task 1¶
Create:
Task 2¶
Create:
Task 3¶
Merge.
Task 4¶
Use commas.
Task 5¶
Create departments.
Task 6¶
Merge three files.
Task 7¶
Serial mode.
Task 8¶
Create CSV.
Command Deep Dive¶
| Command | Purpose | Production Example |
|---|---|---|
paste file1 file2 | Merge two files | Reports |
paste -d "," | CSV output | Data export |
paste -s | Merge lines serially | One-line output |
paste file1 file2 file3 | Merge multiple files | Inventory reports |
Production Troubleshooting Scenario¶
Scenario
An administrator has three files.
servers.txt
status.txt
ip.txt
Tasks:
- Merge all files.
- Create a CSV report.
- Display all servers on one line.
- Save the report.
Solutions:
paste servers.txt status.txt ip.txt
paste -d "," servers.txt status.txt ip.txt
paste -s servers.txt
paste -d "," servers.txt status.txt ip.txt > report.csv
Mini Challenge¶
Create:
employees.txt
salary.txt
department.txt
Perform the following:
- Merge all files.
- Create a comma-separated report.
- Display employee names in a single line.
- Save the merged output.
- Verify the saved file.
Best Practices¶
- Ensure all files have the same number of lines.
- Use
-d ","when generating CSV files. - Use serial mode (
-s) for one-line output. - Combine
pastewithcutandsortfor automation. - Validate merged output before importing into other systems.
Common Mistakes¶
❌ Assuming paste joins files vertically.
✅ It joins them horizontally.
❌ Forgetting that the default delimiter is a tab.
✅ Use:
for CSV files.
❌ Merging files with different line counts.
✅ If one file has fewer lines, the corresponding fields will be empty.
Interview Questions¶
Beginner¶
- What is the purpose of the
pastecommand? - What delimiter does
pasteuse by default? - How do you change the delimiter?
- What does
paste -sdo?
Intermediate¶
- What is the difference between
catandpaste? - How do you merge three files into a CSV?
- What happens when files have different numbers of lines?
- When is
pastepreferable tojoin?
Architect Level¶
- How would you generate a combined infrastructure inventory report using
paste? - Why is
pasteuseful in shell scripting and automation? - How would you combine
cut,sort, andpasteto prepare deployment reports?
Summary¶
In this lesson, you learned:
- Merging files horizontally
- Using custom delimiters
- Creating CSV output
- Using serial mode
- Combining command outputs
- Production report generation
The paste command is a simple but powerful utility for combining related data from multiple files. It is especially useful in shell scripting, report generation, and automation workflows.
Key Takeaways¶
pastemerges files horizontally.- The default delimiter is a tab.
- Use
-dto specify a custom delimiter. - Use
-sto combine all lines from a file into a single line. pasteis commonly used to create CSV reports and combine related datasets.
What's Next?¶
join Command — Joining Files Using a Common Field
In the next lesson, you'll learn:
- Joining files using a common key
- Working with sorted files
- Customizing join fields
- Producing database-style reports
- Real-world inventory and user management examples