linux-commands

Linux paste Command: Merge Files Side by Side

Linux paste Command: Merge Files Side by Side

The Linux paste command combines corresponding lines from files. Give it two lists, and it puts line 1 beside line 1, line 2 beside line 2, and so on.

paste hostnames.txt status.txt

If hostnames.txt contains:

web01
web02
web03

And status.txt contains:

online
offline
maintenance

The output is:

web01	online
web02	offline
web03	maintenance

By default, paste separates the fields with a tab. That makes it useful when two exports are already in the same order and you need a quick side-by-side report. The important warning is just as simple: paste matches line numbers, not IDs. If one file is missing a row, the remaining data can be paired incorrectly without an error.

Quick paste command reference

TaskCommand
Merge two files side by sidepaste hosts.txt status.txt
Use commas between fieldspaste -d, hosts.txt status.txt
Use a pipe delimiter`paste -d'
Put every line from one file on one linepaste -s hosts.txt
Read one input from a pipeprintf '%s\n' web01 web02 | paste - status.txt
Merge three matching filespaste -d, host.txt ip.txt status.txt

The general syntax is:

paste [OPTION]... [FILE]...

paste writes the result to standard output. It does not change the source files unless you deliberately redirect its output over something, which you generally should not do until you have checked the result.

Merge two files side by side

Suppose a monitoring export gives you hostnames in one file and your ticket notes contain the assigned owners in another:

# hosts.txt
app01
app02
db01
# owners.txt
Mina
Devon
Priya

Combine them:

paste hosts.txt owners.txt

Output:

app01	Mina
app02	Devon
db01	Priya

The visible spacing is a tab, even if your terminal makes it look like several spaces. To confirm the separators, use sed -n l on a small sample:

paste hosts.txt owners.txt | sed -n '1,3l'

You will see \t where the tabs occur. That matters when another command expects a literal comma or pipe instead.

For a support ticket, preview the first few rows before saving anything:

paste hosts.txt owners.txt | head

Then write a new file:

paste hosts.txt owners.txt > host-owners.tsv

The .tsv extension is a useful clue that the file contains tab-separated values. It will not enforce a format, but it may keep the next technician from opening the file and wondering why the columns move around.

Change the delimiter with paste -d

Use -d to replace the default tab delimiter.

Create comma-separated output:

paste -d, hosts.txt owners.txt

Output:

app01,Mina
app02,Devon
db01,Priya

For a pipe delimiter, quote it so the shell does not treat | as a pipeline operator:

paste -d'|' hosts.txt owners.txt

Output:

app01|Mina
app02|Devon
db01|Priya

This produces delimited text, but it is not a full CSV or data-validation tool. If a value contains a comma, quote, newline, or your chosen delimiter, paste does not escape it for you. Use a CSV-aware library or tool when handling real spreadsheet exports with quoted fields.

Delimiters with three or more files

With three files, paste needs two separators per output row. A delimiter list cycles as needed:

paste -d',:' hosts.txt ip-addresses.txt status.txt

That puts a comma between the first and second fields, then a colon between the second and third:

app01,192.0.2.10:online

Most of the time, one delimiter is easier for humans and scripts to understand:

paste -d, hosts.txt ip-addresses.txt status.txt

Simple output is usually better during an incident. Nobody wants to reverse-engineer punctuation while a service is down.

Put one file on a single line with paste -s

The -s option works serially. Instead of placing multiple files beside each other, it joins the lines within each file.

paste -s hosts.txt

Output:

app01	app02	db01

Combine -s with a comma delimiter:

paste -sd, hosts.txt

Output:

app01,app02,db01

This is useful for turning a short, trusted list into a command argument or ticket summary. Avoid using command substitution with an unbounded file:

servers=$(paste -sd, hosts.txt)

That is reasonable for ten hostnames. It is a bad plan for a huge export, and it still does not validate the contents. Check the file size and values before feeding the result into another command.

A safe read-only example creates a concise note:

printf 'Checked hosts: %s\n' "$(paste -sd, hosts.txt)"

For shell scripting basics, including quoting and variables, see the Bash scripting guide.

Read from standard input with a dash

A filename of - tells paste to read that input from standard input.

printf '%s\n' web01 web02 web03 | paste - status.txt

Here, the generated hostname list becomes the first input, while status.txt is the second.

You can also number lines before combining them:

seq 1 3 | paste - hosts.txt status.txt

Output:

1	app01	online
2	app02	offline
3	db01	maintenance

The order of the inputs controls the order of the output columns. The dash is not a generic separator; it specifically means “read here from standard input.”

Bash also supports process substitution for command output:

paste <(cut -d, -f1 inventory.csv) <(cut -d, -f3 inventory.csv)

That can be convenient, but <(...) is a Bash feature rather than portable POSIX shell syntax. In scripts, use a Bash shebang intentionally or create temporary files when portability matters. The cut command guide covers simple field extraction and its CSV limitations.

Check line counts before combining files

This is the beginner mistake that causes the most trouble with paste: assuming the rows still align.

Check both files:

wc -l hosts.txt status.txt

If one has 50 lines and the other has 49, stop and investigate. paste will still produce output, filling missing fields with empty text near the end. Worse, if a row disappeared in the middle, both files may have similar-looking data while every later pairing is wrong.

Line counts are necessary, but they are not proof of alignment. When the files contain stable identifiers, compare or join on those identifiers instead of trusting position.

For example, these files should not be pasted blindly:

# inventory.csv
app01,192.0.2.10
app02,192.0.2.11
# status.csv
app02,offline
app01,online

Both have two lines, but their orders differ. A database query, spreadsheet lookup, Python script, or the Unix join command after careful sorting may be more appropriate. Preserve the original files so you can verify the result.

paste versus join, column, and pr

Several Linux commands can make data appear side by side, but they solve different problems.

  • paste combines lines by position. Use it when row 1 in each file genuinely belongs together.
  • join combines sorted text records using a matching field, similar to a simple database join.
  • column formats delimited text for easier reading. It is presentation-focused and may not be installed everywhere.
  • pr formats files for paginated printing and can show files in columns.

If the requirement is “match each hostname to the correct owner,” use a key-aware tool. If the requirement is “place these two already-aligned lists beside each other,” paste is the direct answer.

Real help desk workflow: build a quick host report

Imagine a change window produced three aligned files:

  • hosts.txt contains the expected server names.
  • addresses.txt contains their management IPs.
  • checks.txt contains pass or fail from a health check.

First, confirm the line counts:

wc -l hosts.txt addresses.txt checks.txt

Preview the source files:

head hosts.txt addresses.txt checks.txt

Create a report with a header:

{
  printf '%s\n' 'host,ip,status'
  paste -d, hosts.txt addresses.txt checks.txt
} > health-report.csv

Inspect the result without changing it:

head health-report.csv
wc -l health-report.csv

If the data can contain commas, do not call this reliable CSV. Use a proper CSV writer. If the three files came from different systems or sorting steps, validate the hostname-to-IP relationships independently before attaching the report to a ticket.

This workflow is useful because it is transparent: inputs remain unchanged, output goes to a new file, and every step can be checked. It is much safer than pasting columns manually into a spreadsheet during a busy support shift.

Common paste command mistakes

Mistake 1: Trusting row order without checking

paste does not know that app02 belongs to a specific IP. It only knows both values are on the same numbered line. Compare counts, inspect samples, and use stable keys when correctness matters.

Mistake 2: Forgetting the default delimiter is a tab

Terminal spacing can hide tabs. Use paste -d, when you need commas, or inspect the output with sed -n l.

Mistake 3: Leaving shell metacharacters unquoted

This is wrong for a pipe delimiter:

paste -d| hosts.txt status.txt

The shell sees | as a pipeline. Quote it:

paste -d'|' hosts.txt status.txt

Mistake 4: Treating simple delimited output as safe CSV

paste does not quote fields containing commas or newlines. It is fine for controlled plain text and risky for arbitrary exports.

Mistake 5: Overwriting an input file

Do not do this:

paste hosts.txt status.txt > hosts.txt

The shell may truncate hosts.txt before paste reads it. Write to a new file, verify the result, and move it only when replacement is intentional.

Practice the workflow, not just the syntax

The syntax for paste is short. The actual admin skill is knowing whether the files align, preserving source evidence, choosing a safe delimiter, and checking the output before another system consumes it.

Practice Linux commands in Shell Samurai. It gives you a safe terminal environment for building confidence with pipes, redirection, text files, and the checks that prevent a quick report from becoming a cleanup ticket.

FAQ

What does the paste command do in Linux?

paste combines corresponding lines from one or more files. By default, it separates fields with tabs and writes the result to standard output.

How do I merge two files with a comma delimiter?

Use:

paste -d, first.txt second.txt

This is safe for controlled values that do not contain commas. Use a CSV-aware tool for arbitrary CSV data.

How do I join every line into one comma-separated line?

Use serial mode with a comma delimiter:

paste -sd, file.txt

Does paste match rows by a common field?

No. paste matches lines by position. Use a key-aware tool such as join, a database, or a small script when records must be matched by hostname, ID, or another field.

Does paste change the original files?

No. It writes combined text to standard output. Redirection can save that output, so use a new destination file and verify it before replacing anything.

Practice This in a Real Terminal

Shell Samurai gives you safe Linux missions so the commands actually stick. Chapter 1 is free; the full practice path is a one-time purchase, not another subscription.