column, pr, and fmt - Understanding Text Formatting Commands

column, pr, and fmt - Understanding Text Formatting Commands

What These Three Commands Do

column aligns fields into neat columns, pr paginates and formats text into multiple columns, and fmt reflows paragraph text to a specified width. All three make text more readable and are typically used at the end of a pipeline after filtering with grep or awk.

Quick Summary

  • Align space/tab-delimited data into a tablecolumn -t
  • Display a long list in multiple columnspr -N -t
  • Reflow long lines to a fixed widthfmt -w 72

column - Aligning Fields into a Table

When you cat space- or tab-delimited data, fields don't line up. column -t calculates the maximum width of each field and pads with spaces to align them.

Basic Usage: Align Whitespace-Delimited Data

echo -e "name age city\nAlice 30 Tokyo\nBob 25 Osaka" | column -t
name   age  city
Alice  30   Tokyo
Bob    25   Osaka

Formatting CSV or Colon-Delimited Files

Use -s to specify the delimiter.

head -5 /etc/passwd | column -t -s:
root    x  0  0  root    /root        /bin/bash
daemon  x  1  1  daemon  /usr/sbin    /usr/sbin/nologin
bin     x  2  2  bin     /bin         /usr/sbin/nologin
sys     x  3  3  sys     /dev         /usr/sbin/nologin
sync    x  4  65534  sync  /bin       /bin/sync

The -t -s combination is specific to Linux (util-linux version of column). On macOS (BSD version), -s behavior differs or may not be supported. This article assumes Linux.

Multi-Column Layout with -c

Use -c to specify the page width in characters, and column will arrange entries in multiple columns automatically.

ls /etc | column -c 80
adduser.conf   dhcp           group          magic          protocols
alternatives   environment    hostname       modprobe.d     resolv.conf
apt            fstab          lsb-release    os-release     rsyslog.conf
...

pr - Paginating and Columnating Output

pr was originally designed to format text files for printing, adding headers, footers, and pagination. Today it is most commonly used for multi-column terminal output and side-by-side file comparison.

Multiple Columns

Use -N to specify the number of columns.

ls /usr/bin | pr -3 -t
[                addpart          apt-add-repository
aa-enabled       addr2line        apt-cache
aa-exec          appstreamcli     apt-cdrom
...
  • -3: 3-column layout
  • -t: Suppress the header and footer (filename, date, page number)

Without -t, pr adds a header with the filename, date, and page number. When using it in a terminal, almost always include -t.

Side-by-Side File Comparison

-m merges multiple files side by side.

pr -m -t file1.txt file2.txt

This is a quick way to visually compare two text files without diff markers.

Controlling Width and Lines per Page

pr -3 -t -w 120 -l 60 large_list.txt
  • -w 120: Page width of 120 characters
  • -l 60: 60 lines per page

fmt - Reflowing Paragraph Text

Text copied from the web or generated by programs often has inconsistent line lengths. fmt recognizes paragraph boundaries and reflows text to a specified width.

Basic Usage: Reflow to a Fixed Width

cat long_text.txt | fmt -w 72

The default width varies by implementation; use -w explicitly to be safe.

Paragraph-Aware Reflowing

Blank lines mark paragraph boundaries. fmt reflows each paragraph independently.

cat README.txt | fmt -w 80
This is the first paragraph of the document. It explains what the
program does and how to use it effectively.

This is the second paragraph. It provides additional details about
the configuration options available.

fmt joins lines within a paragraph and reflows them. It treats everything between blank lines as one block, so manually placed line breaks are removed. Do not run fmt on files containing code blocks or bullet lists — it will corrupt the formatting.

Collapsing Extra Spaces with -u

-u (uniform spacing) compresses multiple spaces down to one. Useful for cleaning up text with inconsistent spacing.

echo "This  has   extra   spaces" | fmt -u
This has extra spaces

When to Use Which

Use Case Command
Align space/tab-delimited data as a table column -t
Align CSV/TSV for visual inspection column -t -s,
Display a file list in multiple columns ls | column -c 80
Arrange a long list in 2–3 columns pr -N -t
Compare two text files side by side pr -m -t
Reflow long lines to a fixed width fmt -w 72
Collapse extra whitespace in text fmt -u

Pipeline Patterns

These commands work best at the end of a pipeline as a final formatting step.

# Extract specific CSV columns and display as a table
cut -d, -f1,3,5 data.csv | column -t -s,

# List .conf files in /etc in 3 columns
find /etc -maxdepth 1 -name "*.conf" | pr -3 -t

# Reflow a long git log for easier reading
git log --oneline -20 | fmt -w 72

Next Reading