3.2b grep and Text Extraction Tools
Key Takeaways
- grep prints lines that match a pattern; key options include -i, -v, -n, -r/-R, and -E
- less pages through text; cat dumps files; head and tail show the start or end (tail -f follows a growing log)
- sort orders lines; cut extracts delimited fields; wc counts lines (-l), words (-w), and bytes (-c)
- Realistic pipelines chain generate → filter → slice → count without inventing temporary files
- grep searches file contents; do not confuse it with locating files by name alone
Searching and extracting with the text toolkit (objective 3.2)
With pipes and redirection in place, objective 3.2 centers on grep plus a short list of viewing and shaping tools: less, cat, head, tail, sort, cut, and wc.
grep — match lines by pattern
grep (global regular expression print) prints every line that matches a pattern.
grep error app.log
grep "connection refused" /var/log/syslog
First argument: pattern. Remaining arguments: files (or omit files and read from a pipe).
| Option | Effect | Example |
|---|---|---|
-i | Ignore case | grep -i error app.log |
-v | Invert — lines that do not match | grep -v "^#" config.conf |
-n | Show line numbers | Jump back into an editor |
-r / -R | Recurse directories | grep -rn TODO ~/project/ |
-E | Extended regex | `grep -E "fail |
-c | Count matching lines | Quick frequency check |
grep -n -i timeout app.log
grep -r "TODO" ~/project/
grep -E "fail|error|fatal" app.log
ps aux | grep nginx
Contents vs names: grep looks inside files. A command like grep backup does not list files named backup — it prints lines containing that text. For name-based discovery you use directory tools (covered elsewhere); for Essentials 3.2, stay sharp on content search.
Quote patterns with spaces. Remember default patterns are regexes: escape dots when you mean a literal period (see the previous section).
Worked log search: Start broad, then narrow — grep -ni timeout app.log | head -n 40 samples numbered hits; pipe through grep -v healthcheck | wc -l to drop noise and count. Multi-file greps prefix each hit with the filename (useful for grep -n Listen /etc/apache2/*.conf).
Viewing: cat, less, head, tail
| Tool | Purpose |
|---|---|
cat | Write entire file(s) to the terminal |
less | Page interactively (scroll, search within the view) |
head | Beginning of a file (default first 10 lines) |
tail | End of a file (default last 10 lines) |
cat readme.txt
less /var/log/syslog
head -n 20 app.log
tail -n 50 app.log
tail -f /var/log/syslog # follow — print new lines as they arrive
Use less when cat would flood the screen; head/tail for samples; tail -f to follow a growing log (Ctrl+C to stop).
Pipe grep into a pager when hits are numerous:
grep -n -i error app.log | less
grep -n -i error app.log | head -n 30
Exam trap: follow mode is tail -f, not head -f. cat dumps; it does not page like less.
sort, cut, and wc
sort names.txt # order lines
sort -n numbers.txt # numeric when needed
cut -d: -f1 /etc/passwd # field 1, colon-delimited
cut -d',' -f2,3 data.csv
wc -l app.log
grep -i error app.log | wc -l # count matching lines
| Option | wc counts |
|---|---|
-l | Lines |
-w | Words |
-c | Bytes |
-d / -f on cut select delimiter and fields. Prefer cut for neat columns and grep for arbitrary line patterns. Choose by job: matching lines → grep; column → cut; count → wc; order → sort; window into a long file → head/tail/less.
Worked pipelines (exam-style)
1. Last errors in a log, case-insensitive
tail -n 200 app.log | grep -i error
2. Unique sorted list of usernames from passwd
cut -d: -f1 /etc/passwd | sort
3. Count HTTP 500 lines in an access log
grep " 500 " access.log | wc -l
4. Top of a frequency-style chain (sort, then inspect)
cut -d' ' -f1 access.log | sort | head
5. Config hunt with line numbers
grep -rn "db.example.com" /etc/myapp/ | less
6. Drop noise, then sample: grep -i error app.log | grep -v "benign_code" | head -n 15
7. Count without a temp file: grep -i exception app.log | wc -l (or redirect to a file first if you need to keep the extract).
Tool → purpose map
| Tool | Purpose |
|---|---|
grep | Keep lines matching a pattern |
less | Page through text comfortably |
cat | Dump file contents |
head / tail | First / last lines; tail -f follows |
sort | Order lines |
cut | Extract delimited fields |
wc | Count lines, words, bytes |
Common exam traps
| Story | Wrong instinct | Better tool |
|---|---|---|
| “Which lines contain ERROR?” | Open every file by hand | grep -i ERROR |
| “Show the last 20 lines” | head -n 20 | tail -n 20 |
| “How many matches?” | Guess from the screen | `… |
| “Just the username column” | grep the whole line | cut -d: -f1 |
| “Watch the log grow” | cat in a loop | tail -f |
Exam tips
- Map stories to flags: ignore case →
-i; hide matches →-v; where in the file →-n; whole tree →-r; richer “or” patterns →-E. - Prefer
command | grep patternover inventing temp files. head/taildefault to 10 lines; override with-n.cut -d… -f…for columns;wc -lafter grep for counts.- Keep content search (
grep) separate from “what is this file named?” thinking.
Narrating a one-line pipeline that shows and counts recent errors is exactly what objective 3.2 measures.
Which grep option prints lines that do not contain the pattern?
Which command continuously displays new lines as they are appended to /var/log/app.log?
You need usernames (field 1) from the colon-separated file /etc/passwd. Which command fits best?
What does this pipeline do? grep -i error app.log | wc -l