Linux Text Processing Cheat Sheet

Core Linux text processing commands and shell patterns for searching, filtering, editing, formatting, and analyzing text files and streams.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Overview and Core Patterns

Common text processing building blocks and pipeline patterns.

Pipe file content into grep

Basic pipeline for filtering text from a file.

bashANYcatgreppipeline
bash
cat app.log | grep ERROR

Use a pipeline to send command output into another command for filtering or transformation.

Search a file directly

Prefer direct file arguments when possible.

bashANYgrepsearchfile
bash
grep ERROR app.log

This is more efficient than using `cat file | grep ...` for most cases.

Read from standard input

Many text tools consume stdin naturally.

bashANYstdinawkpipeline
bash
journalctl -u nginx | awk '{print $1, $2, $3}'

Most Linux text tools are designed for stream processing and work especially well in pipelines.

Save output and continue pipeline

Write output to a file while still passing it downstream.

bashANYteepipelinedebugging
bash
grep ERROR app.log | tee errors.txt | wc -l

`tee` is useful when debugging pipelines or capturing intermediate results.

Split a long pipeline across lines

Use backslashes for readability.

bashANYpipelineformattingshell
bash
ps aux 
  | grep python 
  | grep -v grep 
  | awk '{print $2, $11}'

Long shell pipelines become much easier to read and maintain when formatted over multiple lines.

Line Selection and Context

Pick lines, ranges, and context quickly.

Show first 20 lines

Preview the top of a file.

bashANYheadpreview
bash
head -n 20 server.log

Useful for verifying file structure before applying heavier text processing.

Show last 50 lines

Read the end of a file.

bashANYtaillogs
bash
tail -n 50 server.log

Often used with logs to inspect the most recent events.

Follow a log in real time

Watch new lines as they are appended.

bashANYtailfollowlogs
bash
tail -f /var/log/nginx/access.log

Use `-f` to stream appended data in real time.

Print a specific line range

Extract lines 100 through 120.

bashANYsedrangelines
bash
sed -n '100,120p' file.txt

`sed -n` suppresses default output so only the requested range is printed.

Print line range with awk

Alternative line-range extraction using NR.

bashANYawkrangenr
bash
awk 'NR>=100 && NR<=120' file.txt

Awk's `NR` variable tracks the current line number.

Formatting and Cleanup

Normalize spacing, wrap text, and clean lines.

Squeeze repeated spaces

Collapse multiple spaces into one.

bashANYtrspacescleanup
bash
tr -s ' ' < input.txt

Good for cleaning space-delimited text before further parsing.

Trim leading whitespace

Remove spaces and tabs from the start of each line.

bashANYsedtrimwhitespace
bash
sed 's/^[[:space:]]*//' file.txt

Character classes make the expression portable across whitespace types.

Trim trailing whitespace

Remove spaces and tabs from line endings.

bashANYsedtrimwhitespace
bash
sed 's/[[:space:]]*$//' file.txt

Useful before committing scripts, YAML, or source files.

Wrap text to 72 columns

Reflow long text paragraphs.

bashANYfmtwraptext
bash
fmt -w 72 README.txt

`fmt` is useful for quickly wrapping plain text to a target width.

Force hard wraps

Break long lines at a fixed width.

bashANYfoldwraplines
bash
fold -w 80 long-lines.txt

Unlike `fmt`, `fold` wraps mechanically rather than reflowing paragraphs.

Recommended next

No recommendations yet.