col

text processinglinux
The col command is one of the most frequently used commands in Linux/Unix-like operating systems. col The col command filters reverse line feeds and converts whitespace in its input to spaces or tabs. It's primarily used to process text files containing backspace characters and other control characters often generated by certain text processors.

Quick Reference

Command Name:

col

Category:

text processing

Platform:

linux

Basic Usage:

col [options] [arguments]

Common Use Cases

  • 1

    Column formatting

    Format and align text columns

  • 2

    Text processing

    Manipulate text data in pipelines and scripts

  • 3

    Report generation

    Generate reports with formatted columns

  • 4

    Scripting

    Use in shell scripts to format text data programmatically

Syntax

col [OPTION]...

Options

Option Description
-b, --no-backspaces Do not output backspaces, print only the last character written to each column position
-f, --fine Allow half-line line feeds in the output, preserving half-line spacing when possible
-h, --tabs Convert spaces to tabs in the output to maintain formatting with fewer characters
-l, --lines N Buffer N lines in memory (default 128)
-p, --pass Pass unknown escape sequences (starting with \) rather than interpret them
-x, --spaces Convert tabs to spaces in the output
--help Display a help message and exit
--version Output version information and exit

Examples

How to Use These Examples

The examples below show common ways to use the col command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

Basic Examples:

Capture man page and remove backspace characters
man ls | col -b > ls_man.txt
Convert tabs to spaces in a file
cat file_with_tabs.txt | col -x > file_with_spaces.txt
Process groff output and clean up formatting
groff -t -e -mandoc -Tascii document.1 | col -bx > document.txt
Process file with form feed characters
cat file_with_formfeeds.txt | col -f > cleaned_file.txt

Advanced Examples:

Process table through tbl and nroff pipeline
tbl document.tbl | nroff -ms | col -bx > document.txt
Format man page and preserve line breaks
nroff -man source.1 | col -bpx > manpage.txt
Process mathematical equations and tables
eqn math.eqn | tbl | nroff | col -bx > math.txt
Clean up complex formatting from multiple sources
nroff -man -ms document.man | col -bpx > clean_document.txt

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

What Col Does:

  • Filters reverse line feeds from input text
  • Removes backspace formatting when requested (with -b option)
  • Converts between spaces and tabs (depending on options)
  • Handles control characters often found in formatted text
  • Simplifies text formatting produced by text processors like nroff

Common Use Cases:

  • Cleaning up man pages for plain text viewing
  • Converting formatted text to plain text while maintaining alignment
  • Removing control characters and backspace formatting
  • Converting between spaces and tabs for compatibility or file size optimization
  • Processing output from *roff text processors (nroff, troff, groff)

How Backspace Processing Works:

  • Without -b, col simulates backspaces by moving the print position backward
  • With -b, col removes backspaces and the characters they would back over
  • This is useful for converting formatted text (like bold achieved by overstriking) to plain text
  • Backspace processing can significantly clean up the output of manual pages

Line Feed Handling:

  • Normal line feeds move down one line
  • Reverse line feeds move up one line (col handles these specially)
  • Half-line feeds move half a line up or down
  • The -f option affects how col handles half-line feeds

Space and Tab Conversion:

  • With -h, col converts spaces to tabs where appropriate
  • With -x, col converts tabs to spaces
  • These options can't be used together
  • Tab conversion can be useful for file compatibility or size optimization

Memory Usage:

  • Col buffers input in memory to handle reverse line feeds
  • The -l option controls how many lines are buffered
  • For complex formatting with many reverse line feeds, increasing the buffer can be helpful

Related Commands:

  • nroff - Format documents for terminal or character printer output
  • groff - GNU document formatting system (successor to nroff)
  • tbl - Format tables for nroff or troff
  • eqn - Format equations for nroff or troff
  • expand - Convert tabs to spaces
  • unexpand - Convert spaces to tabs
  • man - Display online manual pages

Tips & Tricks

1

Use the -x option to expand tabs

2

Use the -b option to ignore leading whitespace

3

Use the -f option to specify the number of spaces per tab

4

Use the -h option to display help

5

Use the -v option to display version information

Common Use Cases

Column formatting

Format and align text columns

Text processing

Manipulate text data in pipelines and scripts

Report generation

Generate reports with formatted columns

Scripting

Use in shell scripts to format text data programmatically

Data presentation

Present data in a structured and readable format

Related Commands

These commands are frequently used alongside col or serve similar purposes:

Use Cases

1

Column formatting

Format and align text columns

2

Text processing

Manipulate text data in pipelines and scripts

3

Report generation

Generate reports with formatted columns

4

Scripting

Use in shell scripts to format text data programmatically

5

Data presentation

Present data in a structured and readable format

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the col command works in different scenarios.

$ col
View All Commands