100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

sed Interview Questions

The concepts interviewers probe most — the difference between sed and grep/awk, in-place editing, greedy matching, and multi-line handling — framed as questions with model answers.

PracticeIntermediate10 min readJul 10, 2026
Analogies

What Interviewers Are Really Testing

sed questions in a Linux or DevOps interview rarely ask you to recall obscure flags; they probe whether you understand sed's execution model — that it streams input line by line, applies commands to a pattern space, and prints by default. Candidates who grasp this can reason about any question, while those who memorized one-liners stumble the moment the scenario shifts. The strongest answers connect the specific command to why sed behaves that way, and know when a different tool like awk or grep is the better choice.

🏏

Cricket analogy: Understanding the execution model over memorized one-liners is like a batter reading the bowler's wrist rather than pre-deciding a shot — you adapt to whatever is delivered.

sed vs grep vs awk

A classic opener asks you to distinguish the three. grep filters lines by a pattern and prints matches — it selects but does not transform. sed is a stream editor: it transforms text with substitution, deletion, insertion, and can do everything grep does via -n and p, but its sweet spot is editing. awk is a full field-oriented programming language with variables, arithmetic, and per-column logic. The rule of thumb: grep to find, sed to edit line-oriented text, awk when you need columns, math, or accumulated state.

🏏

Cricket analogy: grep is the scout picking which players match a profile, sed is the coach reshaping a player's technique, and awk is the analyst crunching full match statistics.

bash
# grep: find lines (select only)
grep 'ERROR' app.log

# sed: emulate grep, then transform
sed -n '/ERROR/p' app.log                 # print matches like grep
sed 's/ERROR/\x1b[31mERROR\x1b[0m/' app.log   # transform matches

# awk: field logic grep/sed can't easily do
awk '$3 > 500 { print $1, $3 }' access.log   # column math + selection

A frequent follow-up: 'Can sed do what grep does?' Yes — sed -n '/pattern/p' prints matching lines exactly like grep. But grep is faster and clearer for pure searching, and interviewers want to see you pick the simplest tool for the job rather than showing off that sed CAN do it.

Tricky Corners: Greedy Matching and Multi-line

Two questions separate confident candidates from the rest. First, sed's regex is greedy and has no non-greedy quantifier: s/<.*>/X/ on '<a><b>' matches the entire span, not just <a>. The fix is a negated character class like s/<[^>]*>/X/g. Second, sed processes one line at a time by default, so a pattern spanning a newline needs explicit multi-line handling with N to pull the next line into the pattern space. Knowing these limits — and that sed cannot backtrack across lines it has already printed — shows real depth.

🏏

Cricket analogy: Greedy .* grabbing everything is like a fielder over-committing and letting the ball run to the boundary; [^>]* is the disciplined stop at the near cone.

A very common interview trap: candidates claim sed can't do case-insensitive matching. GNU sed supports the I flag on both addresses and the s command (e.g. s/error/warn/gI and /error/Id), but this is a GNU extension — POSIX sed has no such flag. State the capability AND its portability caveat; interviewers reward that nuance.

  • sed streams input line by line into a pattern space and prints by default — the model behind every answer.
  • grep selects lines, sed edits them, awk handles columns, math, and state; pick the simplest fit.
  • sed can emulate grep with -n '/pat/p', but grep is clearer and faster for pure search.
  • sed regex is greedy with no lazy quantifier; use negated classes like [^>]* for precision.
  • sed is single-line by default; use N to pull the next line in for multi-line patterns.
  • GNU sed's case-insensitive I flag is an extension, not POSIX — mention the portability caveat.
  • Strong answers link a command to sed's execution model rather than reciting memorized one-liners.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#SedStreamEditorStudyNotes#SedInterviewQuestions#Sed#Interview#Questions#Interviewers#StudyNotes#SkillVeris#ExamPrep