LearnLinux FoundationsText Processing

paste

paste - a hands-on Linux lab on a real virtual machine.

Merge files side by side with paste, the horizontal partner to cat. The default tab seam, a custom delimiter with -d, and folding one file onto a single line with -s. Learn the length-mismatch gotcha, all in the practice terminal on two tiny files.

Every command you have met so far reads a file from top to bottom. cat prints one file, then the next, stacking them vertically like plates. But some data does not live in one file. It lives in two files that belong side by side: a list of names in one, a list of scores in another, row for row.

Stacking them will not help. You need them welded together LEFT to RIGHT, so line one of the first file sits next to line one of the second. That sideways join is the whole job of one command, and it is called paste.

The black boxes in this lesson are a practice terminal: a safe sandbox that only checks the one command each step teaches, so you cannot break anything. At the end of the module one real Linux machine boots just for you, and you run the full mission there.

For this whole lesson you have two tiny files in the current folder. nums.txt holds three lines, 1, 2, and 3. letters.txt holds three lines, a, b, and c. Keep that picture in your head; every output below comes from exactly those two files.

paste merges files line by line, side by side. Give it two files and it prints one row for every line: the first line of file one, then the first line of file two, then a newline. Then the second lines together, and so on down the files.

Think of cat and paste as a matched pair. cat is vertical: it stacks whole files one under the other. paste is horizontal: it lays files down in columns, next to each other. Same two files, turned ninety degrees.

Point paste at both files, nums.txt first, letters.txt second, and press Enter:

paste nums.txt letters.txt

prompt: student@linuxcamp:~$ answer: paste nums.txt letters.txt output: 1 a 2 b 3 c hint: Type paste, a space, the first file, a space, the second file: paste nums.txt letters.txt

Three rows, each pairing one line from each file: 1 with a, 2 with b, 3 with c. The files went in as two vertical lists and came out as one two-column table. That gap between the number and the letter is not random spacing. It is a single tab character, and the next step is all about it.

Look again at the output: 1, then a stretch of blank space, then a. That blank is exactly one tab character. A tab is a single invisible character that the terminal draws as a jump to the next column stop, which is why the letters all line up in a neat vertical strip.

This matters because the tab is paste's DEFAULT glue. You did not ask for it; paste puts one tab between the columns unless you tell it otherwise. That default is deliberate. A tab almost never appears inside ordinary words, so it makes a clean, unambiguous seam between what came from file one and what came from file two.

When you see columns that line up but the spacing looks uneven if you count the blanks, suspect a tab. One tab, not a run of spaces, is doing the alignment. That is the normal look of paste output.

A tab is a fine default, but often you want something else between the columns, most commonly a comma so the result reads like a spreadsheet row. The -d flag does exactly that. -d is short for delimiter, the character that goes between the joined pieces, and you write the character you want right after it.

To glue the same two files with a comma instead of a tab, use -d,. Run it on the same nums.txt and letters.txt:

paste -d, nums.txt letters.txt

prompt: student@linuxcamp:~$ answer: paste -d, nums.txt letters.txt output: 1,a 2,b 3,c hint: Put -d then the comma with no space, before the files: paste -d, nums.txt letters.txt

Same three rows, but now a comma sits where the tab used to be: 1,a, 2,b, 3,c. That is one tidy comma-separated line per row, the exact shape a spreadsheet expects. The only thing that changed is the glue. -d, told paste to seam the columns with a comma, and everything else stayed identical.

So far paste has read across two files. The -s flag flips its direction entirely. -s is short for serial. With one file it does something different: it takes that file's lines and lays them out on a SINGLE line, joined by the same tab, instead of leaving them stacked.

Give paste -s just one file, nums.txt, and watch its three stacked lines collapse into one row:

paste -s nums.txt

prompt: student@linuxcamp:~$ answer: paste -s nums.txt output: 1 2 3 hint: Add -s before the single file name: paste -s nums.txt

One line now: 1, tab, 2, tab, 3. The three separate lines of nums.txt were folded onto a single row, joined by that same default tab. This is the big shape-change to remember. Without -s, paste reads ACROSS files; with -s, it reads DOWN one file and flattens it. Same command, opposite direction.

Two surprises trip people up, and it is better to meet them here.

First, -s is not a small tweak. It swaps paste from a side-by-side merge of many files into a fold of one file onto one line. If you run paste -s nums.txt letters.txt on both files, you do not get a table. You get two lines, each file folded onto its own row. The shape is completely different from plain paste, so reach for -s only when flattening is truly what you want.

Second, paste matches files line for line, and it does not check that they are the same length. If one file is shorter, the rows past its end simply have a blank where its column would be. paste never errors on a length mismatch; it just leaves the gap. So if a merged column looks half-empty near the bottom, the fix is to check that both files have the same number of lines.

paste prints no error when files have different line counts. It fills the missing cells with nothing and keeps going. A column that trails off into blanks is almost always two files of unequal length, not a broken command.

Scaffolding off. No command is printed this time. You have every piece you need.

You still have nums.txt (1, 2, 3) and letters.txt (a, b, c). You want them merged side by side, one row per line, but glued with a comma instead of the default tab, so each row reads like 1,a. Pick the flag that sets the delimiter, choose the comma, and point paste at both files in order.

prompt: student@linuxcamp:~$ answer: paste -d, nums.txt letters.txt output: 1,a 2,b 3,c hint: The delimiter flag is -d, and the character you want is a comma. Combine them as -d, then the two files: paste -d, nums.txt letters.txt

Comma-joined, from memory. You reached for -d, chose the comma, and named the two files in order, and paste seamed each row with a comma: 1,a, 2,b, 3,c. That is the whole skill: decide what glue you want, pick the flag, and type it without a prompt. It is exactly the move the real machine will ask of you.

You earned this cheat sheet. Every row is a form of paste you have already run:

The one rule to carry away: with no flag, paste reads ACROSS files and glues each row with a tab. -d changes the glue; -s changes the direction, folding one file down onto one line. Tab is always the default seam unless -d says otherwise.

paste is the horizontal partner to cat. Where cat stacks files top to bottom, paste lines them up left to right. Later in the camp you will pair paste with cut, its mirror image: cut slices columns out of a file, and paste welds columns back together. Each of those is its own lesson.

The practice terminal has shown you every form of paste. The default tab merge of two files, the -d flag to choose your own delimiter, and the -s flag to fold one file onto a single line. You typed each one yourself, including the challenge with no command shown.

The Text Processing module ends with one real Linux machine, the text-processing capstone mission, and that is where you merge for real. A VM boots just for you, with its own files. Its objectives use exactly what you practiced across this module: slicing, sorting, counting, translating, and joining text, with paste welding columns together. One difference from here: the mission shows no commands. You read the objective, recall the flag, and type it. That recall is what makes it stick.

Finish the other Text Processing lessons, then go merge some columns for real.

Practice paste in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.