Mastering Linux col: Strip Control Characters & Format Text

Overview

Capturing terminal text streams or documentation from Linux manual pages frequently introduces broken layouts caused by embedded backspaces and terminal escape sequences. This tutorial explores the operational behavior of the col utility on Ubuntu 24.04, detailing flag variations and real-world techniques for preparing clean, readable text files.

Fundamental Concepts & Filtering Backspaces with col

To begin exploring col, verify that the system has bsdextrautils installed, which provides the binary.

Installing bsdextrautils via apt

The col filter strips reverse line feeds, eliminates backspaces, and regularizes whitespace formatting across Unix streams. Executed in its default state without additional switches, it processes backspaced character combinations by superimposing colliding glyphs.
⚙️ [Core Options]

-b : Strips backspace characters completely, retaining only the final character rendered at any column position.
-f : Permits half-line feeds in the forward direction.
-p : Passes through unrecognized control sequences without suppressing them.
-x : Converts hard tab characters into equivalent sequences of space characters.
-l num : Sets the maximum line buffer capacity held in memory.

Testing default col overstrike behavior

Feeding a tab-delimited (\t) string into col outputs cleanly aligned columns across standard terminal stops:

Piping tabbed strings through col

Exporting Clean Man Pages to Plain Text (col -b)

Create an isolated directory to carry out these exercises and switch into it.

Creating and entering the col lab directory

Passing the man ls documentation pipeline through col -b strips out the backspace overstrikes typically used for terminal bolding and underlining, saving the first 30 lines into a clean file named ls_manual_clean.txt.

Extracting sanitized manual pages using col -b

Review the initial 15 lines of ls_manual_clean.txt with head -n 15 to verify that formatting artifacts were removed.

Viewing the top lines of the sanitized manual file

Inspect ls_manual_clean.txt with the file utility to confirm that the file is classified as plain ASCII text without embedded control codes.

Verifying ASCII file format with file command

Expanding Tabs to Spaces for Consistent Layouts (col -x)

To demonstrate tab substitution, generate a structured tabular sample containing \t delimiters and write it to sample_tab.txt.

Writing tab-separated data into a file

Running col -x expands all hard tabs within sample_tab.txt into regular spaces before writing the stream to sample_space.txt. This standardization prevents tabular misalignment when documents are rendered across editors configured with differing tab stop intervals.

Converting tabs to spaces with col -x

Run od -c on sample_tab.txt to examine the file structure at the byte level.
The breakdown reveals literal \t escape characters demarcating each field.

Checking raw tab bytes using od -c

Examine sample_space.txt using od -c to validate the expansion process.
The dump confirms that every \t delimiter has been translated into a series of standard whitespace bytes.

Confirming space expansion with od -c

Preserving Escape Codes and Tuning Buffers (col -p, col -l)

The col -p flag forces the tool to pass ANSI terminal escape sequences—such as color highlight codes—straight through to stdout without filtering. This proves invaluable when processing colored logs where visual markers must remain intact.

Passing ANSI color escapes through col -p

Using col -l 256 defines an explicit memory buffer holding up to 256 lines during processing. Allocating larger buffers guarantees consistent output whenever handling massive streams containing complex reverse line feed requests or vertical layout adjustments.

Processing text using col with custom line buffering

This walkthrough highlighted the core capabilities of col on Ubuntu for stripping terminal artifacts, expanding tabs, and sanitizing text streams. Incorporating these commands into shell pipelines ensures consistent formatting when archiving man pages and parsing complex command outputs.