LearnLinux FoundationsText Processing

column

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

Turn ragged, space-separated data into a clean aligned table with column. The -t table flag that does the aligning, -s to split on a colon, -o to draw a separator, -N for headers, and the fill-mode trap that catches everyone who forgets -t. All in the practice terminal on two tiny fixtures.

Here is a file called table.txt. Three lines, and it is supposed to be a table with three columns: a name, an age, and a city. Print it with cat and look at it honestly.

cat table.txt

prompt: student@linuxcamp:~$ answer: cat table.txt output: name age city alice 30 NYC bob 25 LA hint: Just cat and the filename: cat table.txt

The pieces are all there, but nothing lines up. Because alice is longer than bob, the age and city underneath them start in different places. Your eye has to work to follow a row across. There is one command whose entire job is to fix exactly this, and it is called column.

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. The two small files here, table.txt and people.txt, are built for you, so every output you see below is exact.

column takes text and arranges it into neat, aligned columns. Its most useful trick is turning ragged, space-separated data like table.txt into a clean table where every column lines up in a straight vertical line.

It does this by measuring each column and padding every entry out to the width of the widest one. So if the longest name is alice at five characters, every name gets padded to at least five characters wide, and the ages below them all start at the same spot. The result reads like a spreadsheet instead of a jumble.

column ships as part of util-linux, the same core package that gives you mount, dmesg, and other everyday tools. It has been the quiet way to straighten up terminal output for decades, most often at the end of a chain of other text commands.

The flag that does the aligning is -t, short for table. column -t <file> reads the file, treats runs of spaces as the gaps between fields, and prints the fields back aligned into a table.

Before you run it, picture the result: the same three rows, but with alice, 30, and NYC sitting in tidy vertical columns. Now make it real:

column -t table.txt

prompt: student@linuxcamp:~$ answer: column -t table.txt output: name age city alice 30 NYC bob 25 LA hint: Add the -t flag between column and the filename: column -t table.txt

Now it reads like a table. Look at the left column: name, alice, and bob all start at the same place, and because alice is the widest at five letters, name and bob were padded with spaces so the age column underneath lines up perfectly. Every column got the same treatment. That padding is the whole idea of column -t: measure the widest value in each column, then pad the rest to match.

Not every file uses spaces between fields. Plenty of Linux data files pack their fields together with a single separator character, most commonly a colon. The classic example is /etc/passwd, where each account line looks like root:x:0:0:....

By default column splits on whitespace, so it would treat a whole colon-packed line as one field and align nothing. The -s flag, short for separator, tells column which character marks the boundary between fields instead.

There is a second file here, people.txt, whose three lines are colon-separated: alice:30:NYC, bob:25:LA, carol:41:SF. Tell column to split on the colon and build a table:

column -t -s: people.txt

prompt: student@linuxcamp:~$ answer: column -t -s: people.txt output: alice 30 NYC bob 25 LA carol 41 SF hint: Keep -t for the table, then add -s followed immediately by the colon: column -t -s: people.txt

Three clean columns, and not a colon in sight. -s: told column that the colon is the field boundary, so it broke each line at the colons, then -t aligned the pieces just like before. The separator you name after -s is consumed as the split point, which is why the colons disappear from the output. Swap the colon for a comma with -s, and you could line up a comma-separated file the same way.

This is the mistake almost everyone makes the first time. It is tempting to think column on its own aligns things. It does not. Run column with NO -t and it switches to a completely different mode: instead of aligning fields into a table, it packs the input *lines* side by side across the width of your terminal to save vertical space.

Run the bare form on table.txt and watch it go wrong:

column table.txt

prompt: student@linuxcamp:~$ answer: column table.txt output: name age city alice 30 NYC bob 25 LA hint: Just column and the filename, no -t this time: column table.txt

That is not a table, it is a wreck. With no -t, column took your three separate lines and jammed them onto ONE line, side by side, separated by tab characters (the wide gaps you see). This is the fill mode: it is meant for turning a long single-column list into a compact multi-column block, not for aligning fields. The lesson to burn in: -t is what makes the aligned table. If your columns are not lining up, the first thing to check is whether you forgot -t.

Two more flags round out the tool. -o sets the output separator, the text printed between columns. By default column -t separates columns with two spaces; -o lets you put something visible there instead, like a vertical bar, which makes a table easier to read.

-N (on newer util-linux) sets column names: a comma-separated list of headers printed as a top row above the data. Here is -o in action, drawing a bar between every column of table.txt:

column -t -o ' | ' table.txt

prompt: student@linuxcamp:~$ answer: column -t -o ' | ' table.txt output: name | age | city alice | 30 | NYC bob | 25 | LA hint: Keep -t, then add -o with the separator in quotes: column -t -o ' | ' table.txt

Same aligned table, but now a | sits between each column instead of plain spaces, so the boundaries are obvious. The quotes around | keep the spaces on either side of the bar as one single separator; without the quotes the shell would split it apart. -o never changes the alignment, only what is drawn in the gaps. Its cousin -N NAME,AGE,CITY would print a header row on top with those three labels. Both are polish on the same table -t built.

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

You are handed people.txt again, the file whose three lines are colon-separated (alice:30:NYC and friends). You want it aligned into a clean table, with the colons treated as the boundary between fields, not printed. Combine the flag that builds the table with the flag that sets the separator character.

prompt: student@linuxcamp:~$ answer: column -t -s: people.txt output: alice 30 NYC bob 25 LA carol 41 SF hint: You need -t to align and -s to set the colon separator. Think column, then -t, then -s and the colon, then the file.

That is the everyday move, typed from memory. -t did the aligning and -s: told column the colon was the field boundary, so the three fields lined up into clean columns with the colons stripped out. You combined two flags without a prompt, which is exactly the recall the real machine will ask of you.

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

The one rule to carry away: -t is the flag that aligns. Bare column does something unrelated. Once you have -t, -s chooses the split character, -o chooses what goes between the columns, and -N adds headers.

column almost always sits at the END of a chain. You will use cut, sort, or awk (each its own lesson) to pull and arrange the data, then pipe the result into column -t to line it all up for a human to read. It is the last polish, the tool that makes a wall of text readable.

The practice terminal has shown you every form of column: the -t table that aligns ragged fields, the -s separator for colon-packed files, the -o output separator, the -N headers, and the fill-mode trap that catches everyone who forgets -t. Every one of those you typed yourself.

The Text Processing module ends with one real Linux machine, the text-processing capstone mission, and that is where you straighten real data for yourself. A VM boots just for you, and its objectives use exactly what you practiced across this module: cutting, sorting, and transforming text, then lining the result up with column. 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 straighten a real table for yourself.

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