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
# 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.txtThe 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
1. What does the idiom awk '!seen[$1]++' accomplish?
2. How do you print the last field of each record in AWK?
3. What is the difference between NR and FNR?
4. Which statement about AWK versus grep is correct?
5. In AWK, what happens to $0 when you assign a new value to a field?
Was this page helpful?
You May Also Like
AWK Quick Reference
A compact reference to AWK's structure, special variables, operators, functions, and the most useful one-liners for everyday text processing.
AWK Best Practices
Guidelines for writing AWK programs that are correct, readable, and maintainable, covering quoting, field handling, variable init, and when to move to a script file.
AWK vs Python for Text Processing
A practical comparison of AWK and Python for text and log processing, showing where each tool shines and how to decide between a one-liner and a script.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics