Text-Fu · Lesson 7 of 16
paste
0%
0 / 5 checkpoints answered
Click inside the terminal, type the command, press Enter.
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.
Checkfirst.txt contains A then B, and second.txt contains 1 then 2. What does paste first.txt second.txt produce by default?
press 1/2/3 to answer · → next lesson
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.
CheckWhich command joins corresponding lines from names.txt and roles.txt with a colon?
press 1/2/3 to answer · → next lesson
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.
CheckWhich command joins every line of words.txt into one space-separated output line?
press 1/2/3 to answer · → next lesson
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:
CheckWhat happens when one file passed to parallel paste ends before another?
press 1/2/3 to answer · → next lesson
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
CheckIn producer | paste names.txt -, what does the - operand mean?
press 1/2/3 to answer · → next lesson
To practice merging line-oriented data, try this hands-on lab:
- Simple Text Processing - Learn to use powerful commands like
tr,col,join, andpasteto manipulate and analyze text data efficiently.
Summary
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.
Practice
In the practice terminal below: 1. Two files: printf 'alice\nbob\n' > /tmp/names.txt and printf 'admin\nviewer\n' > /tmp/roles.txt 2. Side by side: paste /tmp/names.txt /tmp/roles.txt It should print alice and admin on one line, then bob and viewer on the next, separated by a tab.
paste lines up matching lines from each file as columns.
Lesson complete
You finished paste. Nice work — keep the momentum going.