The Complete Guide to Formatting Output in Linux: Essential Commands and Techniques

Learn essential Linux output formatting commands including nl, fold, fmt, pr, printf, and groff. Master text manipulation with practical examples and best practices.
code
linux
Author

Steven P. Sanderson II, MPH

Published

January 24, 2025

Keywords

Programming, Formatting Output in Linux, Linux Text Formatting Commands, Linux Command Line Tools, Text Processing in Linux, Linux Output Formatting Techniques, nl Command in Linux, fold Command Usage, fmt Command Examples, pr Command for Printing, printf Command in Linux, How to format text files in Linux command line, Using nl command to number lines in Linux, Practical examples of the fmt command in Linux, Advanced text formatting with groff in Linux, Best practices for formatting output in Linux terminal

Introduction

Text formatting is a crucial skill for Linux users, whether you’re preparing documents for printing, organizing data, or creating readable output. This comprehensive guide will explore the essential Linux commands for formatting output, including nl, fold, fmt, pr, printf, and groff.

Understanding Basic Text Formatting Commands

The nl Command: Line Numbering Made Easy

The nl command is a powerful tool for adding line numbers to text files. Here’s how to use it effectively:

# Basic usage
nl filename.txt

# Number only non-blank lines (default)
nl -b t filename.txt

# Number all lines
nl -b a filename.txt

Key Features:

  • Supports logical page concepts (header, body, footer)
  • Customizable number format and separator
  • Flexible line selection for numbering

The fold Command: Managing Line Width

fold helps wrap text to specific line lengths, essential for formatting text for different display environments:

# Wrap lines to 80 characters
fold -w 80 filename.txt

# Wrap at spaces (avoid breaking words)
fold -s -w 80 filename.txt

The fmt Command: Smart Text Formatting

fmt is a versatile text formatter that handles paragraphs intelligently:

# Format text to 50 characters width
fmt -w 50 filename.txt

# Format while preserving indentation
fmt -c -w 50 filename.txt

Advanced Formatting Tools

The pr Command: Preparing Text for Printing

pr transforms text files for printing with features like:

  • Page headers and footers
  • Multi-column output
  • Page numbering
  • Margin control

Example usage:

# Create paginated output with headers
pr -h "My Document" -l 60 filename.txt

# Create multi-column output
pr -2 filename.txt

The printf Command: Precise Output Control

printf offers C-style formatting capabilities:

# Basic string formatting
printf "Name: %s\nAge: %d\n" "John" 25

# Number formatting
printf "%.2f\n" 3.14159

Common format specifiers:

  • %s - Strings
  • %d - Integers
  • %f - Floating-point numbers
  • %x - Hexadecimal

Document Formatting with groff

Introduction to groff

groff is a powerful document formatting system that can produce:

  • Man pages
  • PDF documents
  • PostScript output
  • ASCII text

Basic example:

# Create a simple formatted document
groff -man -T ascii document.1 > output.txt

Working with Tables in groff

Using the tbl preprocessor:

# Format tables in groff
tbl input.txt | groff -T ascii

Your Turn!

Try this practical exercise:

Problem: Create a formatted table of system information using printf.

Click here for Solution!

Solution:

#!/bin/bash
printf "%-20s %-10s %-15s\n" "HOSTNAME" "MEMORY" "DISK USAGE" $(hostname) "$(free -h | awk '/^Mem:/ {print $2}')" "$(df -h / | awk 'NR==2 {print $5}')"

My Terminal Output

Quick Takeaways

  1. Use nl for line numbering
  2. fold for controlling line width
  3. fmt for paragraph formatting
  4. pr for pagination and printing preparation
  5. printf for precise output control
  6. groff for professional document formatting

FAQs

  1. Q: Which command should I use for simple text wrapping? A: Use fold for basic text wrapping, or fmt if you need more intelligent paragraph formatting.

  2. Q: How can I add line numbers to a file while excluding blank lines? A: Use nl -b t filename.txt

  3. Q: Can I format tables without using groff? A: Yes, you can use printf or column for simple table formatting.

  4. Q: How do I create PDF output from formatted text? A: Use groff to create PostScript output, then convert it using ps2pdf.

  5. Q: What’s the difference between fmt and fold? A: fmt is paragraph-aware and preserves indentation, while fold simply wraps text at specified widths.

References

  1. Linux Handbook - Understanding fold and fmt Commands
  2. GeeksforGeeks - fmt Command in Unix/Linux
  3. Opensource.com - fmt: The Trivial Text Formatter

We hope you found this guide helpful! Please share it if you found it useful, and leave a comment with any questions or suggestions.


Happy Coding! 🚀

Format your text!

You can connect with me at any one of the below:

Telegram Channel here: https://t.me/steveondata

LinkedIn Network here: https://www.linkedin.com/in/spsanderson/

Mastadon Social here: https://mstdn.social/@stevensanderson

RStats Network here: https://rstats.me/@spsanderson

GitHub Network here: https://github.com/spsanderson

Bluesky Network here: https://bsky.app/profile/spsanderson.com

My Book: Extending Excel with Python and R here: https://packt.link/oTyZJ