What is the difference between grep, sed, and awk in Linux?
Understand how grep, sed, and awk differ in Linux — filtering, stream editing, and field-based reporting — with clear examples and when to use each.
Expected Interview Answer
grep searches text and prints lines matching a pattern, sed is a stream editor that transforms text (substitute, delete, insert) line by line, and awk is a field-aware programming language for extracting and reporting on columnar data.
All three read input line by line and are commonly chained in pipelines, but they occupy different jobs. grep answers 'which lines match?', sed answers 'how do I edit those lines in a stream?', and awk answers 'how do I compute or reformat fields within each line?'. awk splits every line into fields ($1, $2, ...) and supports variables, arithmetic, and BEGIN/END blocks, making it the most programmable of the three, while grep and sed are lighter and faster for simple filtering and substitution.
- grep: fast pattern matching and filtering
- sed: in-place stream edits like find-and-replace
- awk: field extraction, arithmetic, and reporting
- All three compose cleanly in shell pipelines
- Available on virtually every Unix-like system
AI Mentor Explanation
Think of a full match commentary log. grep is the analyst who highlights only the deliveries where a wicket fell. sed is the scorer who rewrites 'c&b' into 'caught and bowled' consistently on every line. awk is the statistician who splits each ball into columns, sums the runs per over, and prints a tidy scorecard at the end.
Step-by-Step Explanation
Step 1
Reach for grep to filter
When you only need the lines that match a pattern, grep is the fastest, simplest tool.
Step 2
Reach for sed to transform
When you need to substitute, delete, or insert text as a stream, use sed's s/// and d commands.
Step 3
Reach for awk to compute on fields
When lines are columnar and you need arithmetic, grouping, or reformatting, awk's field variables and BEGIN/END blocks fit best.
Step 4
Chain them in a pipeline
Combine tools: grep to narrow, sed to clean, awk to summarize — each stage passing text to the next.
What Interviewer Expects
- Clear one-line role for each tool
- Awareness that awk is field-aware and programmable
- That sed edits streams, not just replaces once
- Ability to give a piped example combining them
- Knowing when each is the right choice
Common Mistakes
- Saying grep can edit text (it only matches and prints)
- Claiming sed is only for substitution and ignoring delete/insert
- Not knowing awk splits lines into fields automatically
- Using awk for a job grep would do faster
- Forgetting that all three read line by line
Best Answer (HR Friendly)
“grep finds lines that match what you're looking for, sed edits text as it flows through, and awk pulls apart columns to do calculations and build reports. People often use all three together in a single command, each handling one step of the job.”
Code Example
# grep: show only lines mentioning 'error'
grep 'error' app.log
# sed: replace 'error' with 'ERROR' in the stream
sed 's/error/ERROR/g' app.log
# awk: print the 5th field of lines mentioning 'error'
awk '/error/ { print $5 }' app.log
# combined: filter, then sum column 2
grep 'sale' orders.txt | awk '{ total += $2 } END { print total }'Follow-up Questions
- How would you do an in-place edit with sed?
- What do the BEGIN and END blocks in awk do?
- How do grep -E, egrep, and basic regex differ?
- When would you choose awk over cut for field extraction?
- How do you make grep search recursively through directories?
MCQ Practice
1. Which tool automatically splits each input line into fields like $1 and $2?
awk is field-aware and splits every line on whitespace (by default) into $1, $2, and so on.
2. Which command substitutes text in a stream line by line?
sed's s/pattern/replacement/ command performs substitution as text streams through it.
3. What does 'grep' primarily do?
grep searches input and prints the lines that match a given pattern; it does not modify the text.
Flash Cards
What is grep for? — Searching text and printing lines that match a pattern.
What is sed for? — A stream editor for substituting, deleting, and inserting text line by line.
What is awk for? — A field-aware language for extracting columns and computing reports on text.
Which is most programmable? — awk — it has variables, arithmetic, and BEGIN/END blocks.