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

AWK Interview Questions

Common AWK interview questions and the concepts behind them, from records and fields to associative arrays, special variables, and one-liner techniques.

PracticeIntermediate10 min readJul 10, 2026
Analogies

What Interviewers Probe

AWK interview questions test whether you understand the execution model, not just syntax. The classic opener is to explain the pattern-action paradigm: AWK reads input record by record, and for each record it evaluates patterns and runs the action of any that match. Interviewers then check that you know the special blocks (BEGIN runs before input, END after), the automatic variables (NR, NF, FS, OFS, RS, ORS), and how fields $1..$NF and the whole record $0 relate. Demonstrating that you can reason about when each block fires separates confident users from those who memorized snippets.

🏏

Cricket analogy: Explaining the execution model is like a commentator describing not just the shot but the field placement and match situation — showing you understand the whole game, not one ball.

The Special Variables

A frequent question is to list and explain AWK's built-in variables. NR is the running record number across all input; FNR resets per file. NF is the number of fields in the current record, so $NF is the last field. FS and OFS are the input and output field separators; RS and ORS are the record separators. A strong answer also notes that $0 is the entire record and that changing a field reassigns $0 using OFS. Interviewers love the follow-up: how do you print the last field? Answer: print $NF. And the second-to-last? print $(NF-1).

🏏

Cricket analogy: NR versus FNR is like the tournament run tally versus a single match's score — one keeps counting across games, the other resets each match.

Classic One-Liner Questions

bash
# Print lines longer than 80 characters
awk 'length($0) > 80' file.txt

# Print the number of lines (like wc -l)
awk 'END { print NR }' file.txt

# Sum the values in column 2
awk '{ s += $2 } END { print s }' file.txt

# Print unique values of column 1 (like sort | uniq, order-preserving)
awk '!seen[$1]++' file.txt

# Swap columns 1 and 2
awk '{ tmp = $1; $1 = $2; $2 = tmp; print }' OFS='\t' file.txt

The idiom !seen[$1]++ is a favorite interview question. It works because seen[$1]++ returns the current count (0 the first time, so the ! makes it true and the line prints) then increments. On repeats the count is non-zero, ! makes it false, and the line is skipped — giving order-preserving de-duplication in eight characters.

A common trap: candidates say AWK and grep are interchangeable. grep only filters lines; it cannot split fields, do arithmetic, or maintain state across lines. Conversely, do not claim AWK replaces sed for pure stream editing — sed is often terser for simple substitutions. Know each tool's niche, because interviewers probe whether you reach for the right one.

  • Be ready to explain the pattern-action model and when BEGIN and END fire.
  • Know the special variables: NR, FNR, NF, FS, OFS, RS, ORS, and $0.
  • Print the last field with $NF and the second-to-last with $(NF-1).
  • Understand that changing a field rebuilds $0 using OFS.
  • Master idioms like !seen[$1]++ for order-preserving de-duplication.
  • Articulate how AWK differs from grep and sed and when to use each.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#AWKStudyNotes#AWKInterviewQuestions#AWK#Interview#Questions#Interviewers#StudyNotes#SkillVeris#ExamPrep