Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Text Processing

If the Pipes & Redirection section taught you how to wire commands together, this section teaches you the tools that make the wiring worth doing. Text is the common currency of Linux: configuration files, logs, command output, source code — nearly everything is plain text, and nearly everything useful is text you want to search, filter, reshape, or count.

This section covers the classic text-processing toolkit. Each command does one thing well, and together they can turn a 10,000-line log into a one-line answer. grep finds lines. sed and awk rewrite and analyze them. sort, uniq, and wc count and organize them. tr, cut, and paste reshape them character by character or column by column. tee splits a stream, and xargs turns text into arguments for other commands.

Common Text Processing Tools

CommandWhat It Does
grepSearch text for lines matching a pattern.
sedFind and replace text, edit streams line by line.
awkA full programming language for columnar text analysis.
sortSort lines of text alphabetically or numerically.
uniqReport or remove duplicate lines.
wcCount lines, words, and characters.
trTranslate or delete characters.
cutExtract specific columns or characters from each line.
pasteMerge lines of files side by side.
teeWrite output to a file and the screen at the same time.
xargsBuild and run commands from piped input.

The Classic Pipeline

Almost every real-world text task is a pipeline built from these tools. A favorite: finding the ten most common words in a file.

cat file.txt | tr ' ' '\n' | sort | uniq -c | sort -rn | head -10

Read it left to right: split into words, sort them, collapse duplicates with counts, sort by count descending, take the top ten. Five tiny tools, one powerful result — that is the whole philosophy of this section.

Why This Matters

  • Logs are unreadable without them. No one reads a 2 GB log file by eye; you grep and awk it.
  • You stop editing by hand. sed applies the same fix to a hundred files in one command.
  • Counting becomes trivial. “How many errors today?” is one pipeline, not an afternoon.
  • Other tools depend on them. find with -exec uses xargs; almost every admin script uses grep.

Learn these eleven commands and you have learned the vocabulary that the rest of the Linux world speaks.