LinuxII · Shell and Command-Line OperationsText tools
Core text tools — grep, cut, sort, uniq, tr, wc
What you'll learn
- Use grep with -E, -F, -i, -v, -c, -l, -n, -o, and -P correctly
- Use cut, sort, uniq, tr, and wc in production-style pipelines
- Choose the right tool for the structure of the input
- Identify when to reach for awk or sed instead
Prerequisites
Verified against Ubuntu 24.04 LTS · Debian 12 (Bookworm) · RHEL 9.x · Rocky Linux 9.x · AlmaLinux 9.x · Linux kernel 6.1 LTS / 6.6 LTS · systemd 255+ · OpenSSH 8.7p1 (RHEL 9) / 9.6p1 (Ubuntu 24.04) · nftables 1.0.x · chrony 4.x · Pacemaker 2.1.x · Corosync 3.1.x · 2026-08-09
Six text tools cover 80% of production log-and-data triage. This lesson is the muscle-memory foundation that the next two lessons (sed, awk; find, xargs) build on.
grep — find lines that match
The most-used flags in production:
| Flag | Effect |
|---|---|
-i | Case-insensitive |
-v | Invert: print non-matching lines |
-c | Count matches per file |
-l | List filenames that have at least one match |
-n | Print line numbers |
-o | Print only the matched text, not the whole line |
-E | Extended regex (egrep) |
-F | Fixed string, no regex |
-P | Perl-compatible regex (PCRE) — fastest for complex patterns |
-A N | Print N lines of context after each match |
-B N | Print N lines of context before each match |
-C N | Print N lines of context around each match |
--color=auto | Highlight matches in interactive use |
-r | Recurse into directories |
-R | Like -r but follows symlinks |
$ grep -n 'systemd' /etc/group | head -342:systemd-journal:x:101:
43:systemd-network:x:102:
44:systemd-resolve:x:103:Illustrative output
cut — slice by column
cut extracts columns from delimited text. Three modes:
$ grep '^root:' /etc/passwd | cut -d: -f1,7root:/bin/bashIllustrative output
$ uname -a | cut -c1-10Linux 6.6.Illustrative output
sort — order lines
$ grep '^root:' /etc/passwd; printf '\n'; grep -v '^root:' /etc/passwd | sort -t: -k3 -n | head -3root:x:0:0:root:/root:/bin/bash
abrt:x:173:173::/etc/abrt:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
avahi:x:120:124:Avahi mDNS daemon:/run/avahi:/sbin/nologinIllustrative output
Useful flags: -r reverse, -u unique (sort + dedupe), -h human
numeric sort (-h understands 1K, 2G), -V version sort, -R
random, -s stable (disable last-resort comparison).
uniq — collapse adjacent duplicates
$ printf 'a\nb\nb\nc\nb\n' | uniq -c 1 a
2 b
1 c
1 bIllustrative output
tr — translate or delete characters
$ echo "Hello, World!" | tr 'a-z' 'A-Z'HELLO, WORLD!Illustrative output
$ printf 'a\nb\nc\n' | tr -d '\n'; echoabcIllustrative output
$ echo "hello world" | tr -s ' 'hello worldIllustrative output
wc — word, line, character, byte count
$ wc /etc/passwd 46 92 3010 /etc/passwdIllustrative output
The classic top-N pipeline
Many production diagnostics start with this shape:
$ journalctl -u ssh --since '1 day ago' --no-pager 2>/dev/null | grep -oE 'from [0-9.]+' | sort | uniq -c | sort -rn | head -10...Illustrative output
Knowledge check
Knowledge check · 3 questions
Q1. Which grep flag prints only the matched text, not the entire matching line?
Q2. uniq collapses duplicate lines wherever they appear in the input.
Q3. Which of the following patterns correctly sort /etc/passwd by numeric UID? Select all that apply.
Passing score: 75%. Answers are checked in this browser.