paste
100%

Text-Fu · Lesson 7

paste

Learn how to merge corresponding lines or serialize lines with configurable delimiters using paste.

The paste command combines lines as columns. By default, it takes one line from each input file, joins those lines with a tab, and repeats until all inputs reach end-of-file.

Merging Files Side by Side

Create two small files:

$ printf 'alice\nbob\n' > names.txt
$ printf 'admin\nviewer\n' > roles.txt

Pass both files to paste:

$ paste names.txt roles.txt
alice	admin
bob	viewer

The visible spacing between columns is a tab. Unlike cat, which writes one complete file after another, paste combines corresponding input lines.

first.txt contains A then B, and second.txt contains 1 then 2. What does paste first.txt second.txt produce by default?

Choosing a Delimiter

Use -d LIST to replace the default tab separator. For a colon:

$ paste -d ':' names.txt roles.txt
alice:admin
bob:viewer

Quote delimiters that have shell meaning. paste can cycle through multiple delimiter characters when the list contains more than one, but a single character is easiest when building two columns.

Which command joins corresponding lines from names.txt and roles.txt with a colon?

Serializing Lines from One File

The -s option processes each input file serially, joining its lines into one output line. Create a file with one word per line:

$ printf 'The\nquick\nbrown\nfox\n' > words.txt
$ paste -s words.txt
The	quick	brown	fox

Combine -s with -d to choose the separator:

$ paste -s -d ' ' words.txt
The quick brown fox

If several files are supplied with -s, each file becomes its own output line.

Which command joins every line of words.txt into one space-separated output line?

Handling Unequal Input Lengths

When parallel input files have different numbers of lines, paste continues until the longest file ends. Missing values from a shorter file become empty fields:

$ printf 'A\nB\nC\n' > letters.txt
$ printf '1\n2\n' > numbers.txt
$ paste -d ':' letters.txt numbers.txt
A:1
B:2
C:

What happens when one file passed to parallel paste ends before another?

Reading One Input from stdin

Use - as a file operand to read that position from stdin:

$ printf 'admin\nviewer\n' | paste -d ':' names.txt -
alice:admin
bob:viewer

In producer | paste names.txt -, what does the - operand mean?

To practice merging line-oriented data, try this hands-on lab:

  1. Simple Text Processing - Learn to use powerful commands like tr, col, join, and paste to manipulate and analyze text data efficiently.

Lesson complete

You finished paste

You can now combine line-oriented inputs with predictable alignment and delimiters.

  • Merge corresponding lines from several files.

  • Replace the default tab separator with -d.

  • Serialize one file's lines with -s.

  • Interpret empty fields from shorter inputs.

  • Use - when one input comes from stdin.

Keep your learning progress

Create a free account to save this lesson and continue learning on any device.

Create a free account
Next Lesson
Back to Text-Fu