InterviewPitch
AWK interview questions

AWK Interview Questions with Answers

Most Asked AWK Interview Questions for Software Engineer Roles

100+ QuestionsDetailed AnswersCode ExamplesUpdated for 2026

Introduction

This page provides a complete collection of AWK Interview Questions and Answers designed for frontend developers, full-stack developers, React developers, Angular developers, and software engineers preparing for technical interviews. AWK is a strongly typed programming language developed by Microsoft that extends JavaScript by adding static typing, interfaces, advanced type checking, and modern development features. It helps developers build scalable and maintainable applications. This interview guide covers beginner, intermediate, and advanced AWK concepts including types, interfaces, classes, generics, decorators, utility types, modules, AWK with React, Angular, Node.js, and real-world coding interview scenarios.

Why AWK?

  • Strong static typing – catches errors at compile time, reducing runtime bugs
  • Excellent tooling and IDE support – autocompletion, navigation, and refactoring
  • Superset of JavaScript – works seamlessly with all existing JavaScript libraries
  • Used by major frameworks like React, Angular, and Vue – essential for large-scale apps
  • Enables scalable and maintainable enterprise-grade applications
  • Growing community, continuous improvements, and high demand in the job market

Most Asked AWK Interview Questions

Beginner
1. What is AWK?

AWK is a powerful text-processing, data-extraction, and pattern-scanning programming language that is standard on Unix and Linux-like operating systems. It works by checking lines of a file, matches particular pattern rules, and runs corresponding scripting instructions.

Beginner
2. Who developed AWK?

AWK was designed and implemented in 1977 by three legendary computer scientists: Alfred Aho, Peter Weinberger, and Brian Kernighan. The name of the language is an acronym derived from the first letters of their last names.

Beginner
3. What is the basic syntax of AWK?

The standard structure consists of a pattern definition followed by an action enclosed inside curly braces:

AWK
awk 'pattern { action }' file
Beginner
4. What is a pattern in AWK?

A pattern acts as a gatekeeper rule or filtering condition. AWK loops through input lines and runs actions only if a line matches the pattern (e.g., regex filters, numerical comparisons, or target words).

Beginner
5. What is an action in AWK?

An action is a sequence of statements enclosed in curly brackets { ... }. When a pattern is matched, AWK runs these statements (e.g., calculations, custom formatting, or text outputs).

Beginner
6. What does $0 represent?

In AWK, $0 is a special variable representing the entire current line of input record in its original un-split state:

AWK
awk '{ print $0 }' file.txt
Beginner
7. What does $1, $2 represent?

AWK splits records into column columns dynamically. $1 stores the first field, $2 is the second, and so on:

AWK
awk '{ print $1, $2 }' file.txt
Beginner
8. What is FS?

FS stands for Field Separator. It defines the character used to split incoming records into fields. The default FS is whitespace (spaces and tabs):

AWK
awk 'BEGIN { FS=":" } { print $1 }' file.txt
Beginner
9. What is OFS?

OFS stands for Output Field Separator. It specifies the delimiter used when printing fields separated by commas in print statements:

AWK
awk 'BEGIN { OFS=" | " } { print $1, $2 }' file.txt
Beginner
10. What is NR?

NR stands for Number of Records. It tracks the total count of lines processed so far, functioning like a current loop iteration counter:

AWK
awk '{ print NR, $0 }' file.txt
Beginner
11. What is NF?

NF is a built-in variable containing the Number of Fields in the current line record. It is highly useful for checking line complexity or printing specific end elements:

AWK
awk '{ print NF }' file.txt
Beginner
12. What is BEGIN block?

The BEGIN block represents an initialization phase. Statements placed inside a BEGIN block execute exactly once before AWK reads any lines of the input file:

AWK
awk 'BEGIN { print "Processing Start..." } { print $1 }' file.txt
Beginner
13. What is END block?

The END block runs exactly once after AWK has finished processing all lines of input records. It is typically used for printing final summaries, calculated totals, or averages:

AWK
awk '{ print $1 }' END { print "Total Lines Processed:", NR } file.txt
Beginner
14. How to print a file using AWK?

By default, calling print with no parameters outputs the entire current line record:

AWK
awk '{ print }' file.txt
Beginner
15. How to print specific column?

You can print targeted fields by passing variable indices like $1 or $2 directly to print commands:

AWK
awk '{ print $2 }' file.txt
Intermediate
16. How to set custom delimiter?

Use the -F command-line argument to specify a custom character string as the field delimiter:

AWK
awk -F',' '{ print $1, $3 }' file.csv
Intermediate
17. How to print lines containing a word?

Pass the word inside regular expression slashes (/pattern/) before the action execution block:

AWK
awk '/error/' file.txt
Intermediate
18. How to print lines greater than value?

Use comparison operators (>, <, ==) to evaluate field column values:

AWK
awk '$3 > 100' file.txt
Intermediate
19. How to count lines?

By outputting the final NR value inside an END block, you get the overall number of lines processed:

AWK
awk 'END { print NR }' file.txt
Intermediate
20. How to sum a column?

Accumulate column field values iteratively inside variables, then output the final calculation state in the END block:

AWK
awk '{ sum += $2 } END { print sum }' file.txt
Intermediate
21. How to print line numbers?

By printing the record variable NR alongside the original string $0, you get line numbers prefixing each record:

AWK
awk '{ print NR, $0 }' file.txt
Intermediate
22. How to print last field?

Access the final field element dynamically using $NF, which acts as a pointer index to the total number of fields:

AWK
awk '{ print $NF }' file.txt
Intermediate
23. How to replace text?

Use the built-in gsub() utility to swap matches globally on line strings:

AWK
awk '{ gsub(/old/, "new"); print }' file.txt
Intermediate
24. How to print even lines?

Check if the current line record number NR is divisible by 2 using modulus operations:

AWK
awk 'NR % 2 == 0' file.txt
Intermediate
25. How to print odd lines?

Filter line records using the modulus operation matching non-zero remainder states:

AWK
awk 'NR % 2 == 1' file.txt
Intermediate
26. What is RS?

RS is the Record Separator. It controls how input data is divided into logical records (default is newline \n). Setting RS to empty ("") enables multi-line paragraph parsing modes:

AWK
awk 'BEGIN { RS="" } { print $1 }' file.txt
Intermediate
27. How to use conditions?

Place string or numeric conditional checks as patterns before action blocks to selectively process lines:

AWK
awk '$2 == "Admin"' file.txt
Intermediate
28. How to use if statement?

Use standard if-else constructs inside action blocks to create branching logic paths:

AWK
awk '{ if ($3 > 50) print $1, "Passed"; else print $1, "Failed" }' file.txt
Intermediate
29. How to use loops?

Run standard loop loops (for, while) directly inside action blocks to parse field segments cleanly:

AWK
awk '{ for (i = 1; i <= NF; i++) print "Field", i, "is", $i }' file.txt
Intermediate
30. How to format output?

Use the formatting utility printf to configure tabular columns, padding alignments, and custom layouts:

AWK
awk '{ printf "%-10s %5d\n", $1, $2 }' file.txt
Intermediate
31. What is associative array?

An associative array is a map index system backed by string keys instead of numbers. This allows developers to build lookup structures matching terms, counts, or keys directly.

Intermediate
32. How to count duplicate values?

By mapping string values into associative arrays, you can easily track occurrence frequencies across fields:

AWK
awk '{ count[$1]++ } END { for (val in count) print val, count[val] }' file.txt
Intermediate
33. What is getline?

The getline function explicitly reads the next line of input immediately, allowing developers to control line iteration manually within custom control logic loops.

Intermediate
34. How to ignore case?

Configure the system variable IGNORECASE = 1 inside your BEGIN block to make regular expressions case-insensitive:

AWK
awk 'BEGIN { IGNORECASE = 1 } /error/' file.txt
Intermediate
35. How to print first and last line?

Check the record count NR for 1 to catch the first line, then run the END block to catch the final record:

AWK
awk 'NR == 1 { print "First:", $0 } END { print "Last:", $0 }' file.txt
Advanced
36. How to pass variables?

Use the -v argument flag to map shell environment variables into AWK parameters before script parsing begins:

AWK
awk -v threshold=100 '$3 > threshold' file.txt
Advanced
37. How to merge two files?

Track index states using NR == FNR to cache the first file in memory, then map values against the second target:

AWK
awk 'NR == FNR { a[$1] = $2; next } { print $0, a[$1] }' file1 file2
Advanced
38. What is gsub()?

The gsub() function performs string replacement globally across matched target strings. It modifies the variable parameter references directly in memory.

Advanced
39. What is sub()?

Unlike `gsub()`, the sub() function is non-global. It targets and replaces only the very first occurrence of a matched pattern on structural variables.

Advanced
40. How to delete empty lines?

By checking the field count variable NF, AWK automatically filters out and skips blank line inputs:

AWK
awk 'NF' file.txt
Coding Round
41. Print 2nd column sorted

Isolate column columns using AWK, then pass the pipe stream into the terminal utility sort:

AWK
awk '{ print $2 }' file.txt | sort
Coding Round
42. Find max value in column

Calculate maximum column values efficiently by running variable check bounds iteratively:

AWK
awk 'BEGIN { max = 0 } $3 > max { max = $3 } END { print "Max:", max }' file.txt
Coding Round
43. Reverse file content

Load all line strings sequentially into an array, then iterate backwards through the array in the END block:

AWK
awk '{ lines[NR] = $0 } END { for (i = NR; i >= 1; i--) print lines[i] }' file.txt
Coding Round
44. Remove duplicates

Filter repeating strings quickly using associative index increments with boolean patterns:

AWK
awk '!seen[$0]++' file.txt
Coding Round
45. Count words in file

Track dynamic word counts by accumulating the NF values of each input record:

AWK
awk '{ total += NF } END { print "Words:", total }' file.txt
Coding Round
46. Extract lines between patterns

Identify and output records belonging strictly between specific starting and ending pattern definitions:

AWK
awk '/start/,/end/' file.txt
Coding Round
47. Print CSV column

Split standard comma-separated inputs safely using custom field delimiters:

AWK
awk -F',' '{ print $2 }' file.csv
Coding Round
48. Replace delimiter

Swap delimiters globally by configuring matching variables inside BEGIN blocks:

AWK
awk 'BEGIN { FS=","; OFS="|" } { $1=$1; print }' file.csv
Coding Round
49. Count specific word

Parse fields iteratively across columns to match and count exact target words:

AWK
awk '{ for (i=1; i<=NF; i++) if ($i == "target") count++ } END { print "Occurrences:", count }' file.txt
Coding Round
50. Print unique first column

Map and isolate unique string parameters across columns while skipping repeated entries:

AWK
awk '!seen[$1]++ { print $1 }' file.txt
Advanced
51. How to use the length() function?

length() returns the number of characters in a string or the length of a field.

AWK
awk '{ print length($0) }' file.txt
Advanced
52. How to use the substr() function?

substr(string, start, length) extracts a substring.

AWK
awk '{ print substr($1, 1, 3) }' file.txt
Advanced
53. How to use the split() function?

split(string, array, separator) splits a string into an array.

AWK
awk '{ split($0, arr, ","); print arr[1] }' file.txt
Advanced
54. How to use match() and retrieve RSTART/RLENGTH?

match(string, regex) returns the position where the regex matches, and sets RSTART and RLENGTH.

AWK
awk '{ if (match($0, /[0-9]+/)) print RSTART, RLENGTH }' file.txt
Advanced
55. How to use the index() function?

index(string, substring) returns the position of the first occurrence.

AWK
awk '{ print toupper($0) }' file.txt
Advanced
56. How to change case using toupper()/tolower()?

toupper() and tolower() convert strings to uppercase/lowercase.

AWK
awk '{ print sprintf("%-10s %5d", $1, $2) }' file.txt
Advanced
57. How to format strings with sprintf()?

sprintf(format, ...) works like printf but returns the formatted string.

AWK
awk 'BEGIN { system("echo Hello") }'
Advanced
58. How to run a shell command with system()?

system(command) executes a shell command and returns its exit status.

AWK
awk '{ print $0 > "output.txt" }' file.txt
Advanced
59. When and how to use close()?

close(filename) closes an open file or pipe to free system resources or prevent "too many open files" errors.

AWK
awk 'BEGIN { while (getline < "data.txt") print }'
Advanced
60. How to use getline with a file?

getline < "file" reads the next line from that file.

AWK
awk '{ getline var; print var }' file.txt
Advanced
61. How to use getline into a variable?

getline var reads the next line into the variable var.

AWK
awk '{ getline arr[NR] }' file.txt
Advanced
62. Can getline populate an array element?

Yes, you can assign to an array element: getline arr[NR].

AWK
awk 'BEGIN { print ENVIRON["HOME"] }'
Advanced
63. How to access environment variables using ENVIRON?

The ENVIRON array holds environment variables keyed by name.

AWK
awk 'BEGIN { for (i=1; i<ARGC; i++) print ARGV[i] }' file1 file2
Advanced
64. What are ARGC and ARGV?

ARGC is the number of command-line arguments, and ARGV is an array of the arguments.

AWK
awk '{ print FILENAME, FNR, $0 }' file1 file2
Advanced
65. How to use FILENAME and FNR?

FILENAME is the current input file name, and FNR is the record number within that file.

AWK
awk '{ print CONVFMT }' file.txt
Advanced
66. What is CONVFMT and how to use it?

CONVFMT controls the conversion of numbers to strings (default "%.6g").

AWK
awk 'BEGIN { OFMT="%.2f"; print 3.14159 }'
Advanced
67. How to change the output numeric format with OFMT?

OFMT sets the format for printing numbers (similar to CONVFMT but for output).

AWK
awk 'BEGIN { RS=""; ORS="\n\n" } { print $0 }' file.txt
Advanced
68. How to change record separators with RS and ORS?

RS (input record separator) and ORS (output record separator) can be set to any string.

AWK
awk '{ print RT }' file.txt
Advanced
69. What is RT (record terminator)?

RT holds the actual text that matched the record separator (RS).

AWK
awk '{ print RSTART, RLENGTH }' file.txt
Advanced
70. How are RSTART and RLENGTH used?

After match(), RSTART holds the start index and RLENGTH the length of the match.

AWK
awk '{ print SUBSEP }' file.txt
Advanced
71. What is SUBSEP and how does it affect multidimensional arrays?

SUBSEP is the separator used to construct the subscript string for multidimensional arrays (default "\034").

AWK
awk '{ arr[1] = "a"; arr[2] = "b"; for (i in arr) print i, arr[i] }'
Advanced
72. How to sort an array in AWK?

Use asort() (gawk) to sort array values; asorti() sorts indices.

AWK
awk '{ arr[1] = "x"; arr[2] = "y"; asort(arr); for (i in arr) print arr[i] }'
Advanced
73. How to delete an array element?

Use delete array[index] to remove a specific element.

AWK
awk '{ arr[1] = "z"; delete arr[1]; for (i in arr) print }'
Advanced
74. What does nextfile do?

nextfile skips the rest of the current input file and moves to the next one.

AWK
awk '{ if (NR > 10) nextfile }' file.txt
Advanced
75. How to exit from an AWK script?

exit terminates the script immediately; the END block still runs.

AWK
awk '{ if (NR > 5) exit } END { print "Exited" }' file.txt
Advanced
76. How to redirect output to a file?

Use > "filename" to overwrite or >> to append.

AWK
awk '{ print $0 > "out.txt" }' file.txt
Advanced
77. How to append to a file?

Use >> "filename" to append output.

AWK
awk '{ print $0 >> "out.txt" }' file.txt
Advanced
78. How to pipe output to a shell command?

Use | "command" to send data to a shell command.

AWK
awk '{ print $0 | "sort" }' file.txt
Advanced
79. How to run AWK script from a file?

Use the -f option: awk -f script.awk input.txt.

AWK
awk 'BEGIN { print "Start" } { print } END { print "End" }' file.txt
Advanced
80. How to include another AWK script (gawk)?

In gawk, use @include "other.awk" inside the script.

AWK
awk -f script.awk file.txt
Coding Round
81. How to analyze a web server log with AWK?

Extract fields like IP, status, and bytes; count occurrences, sum bytes, etc.

AWK
awk '@include "other.awk"' file.txt
Coding Round
82. How to generate a report with AWK?

Use printf to format columns, compute totals in END block.

AWK
awk '{ for (i=1; i<=NF; i++) if ($i ~ /^[0-9]+$/) print $i }' file.txt
Coding Round
83. How to merge two files on a common key?

Read first file into an array, then process second file and look up values.

AWK
awk 'NR==FNR { a[$1]=$2; next } { print $0, a[$1] }' file1 file2
Coding Round
84. How to validate numeric fields in a CSV?

Check each field with a regex (~ /^[0-9]+$/) and flag errors.

AWK
awk '{ if (length($0) > 100) print "Long line:", NR }' file.txt
Coding Round
85. How to replace all occurrences of a pattern?

Use gsub(/old/, "new") to replace globally.

AWK
awk '{ gsub(/old/, "new"); print }' file.txt
Coding Round
86. How to sum a column and print the total?

Accumulate the field in a variable and print in END block.

AWK
awk '{ sum += $2 } END { print "Total:", sum }' file.txt
Coding Round
87. How to filter lines where a field is greater than a value?

Use a pattern like $3 > 100 as the condition.

AWK
awk '{ if ($3 > 100) print $0 }' file.txt
Coding Round
88. How to print lines matching a pattern (like grep)?

Use /pattern/ or $0 ~ /pattern/ as the pattern.

AWK
awk '{ if ($0 ~ /pattern/) print }' file.txt
Coding Round
89. How to print only even lines?

Use NR % 2 == 0 as the pattern.

AWK
awk 'NR % 2 == 0' file.txt
Coding Round
90. How to reverse the order of fields in each line?

Loop from NF down to 1 and print each field.

AWK
awk '{ for (i=NF; i>=1; i--) printf "%s ", $i; printf "\n" }' file.txt
Coding Round
91. How to sort lines by the second field?

Extract the second field and pipe to sort -k2.

AWK
awk '{ print $1, $2 }' file.txt | sort -k2
Coding Round
92. How to handle multi-line records (e.g., paragraphs)?

Set RS="" to treat blank lines as record separators, and FS="\n" to split fields by newline.

AWK
awk 'BEGIN { RS=""; FS="
" } { print $1, $2 }' file.txt
Coding Round
93. How to count frequency of values in a column?

Use an associative array to count occurrences and print in END block.

AWK
awk '{ count[$1]++ } END { for (key in count) print key, count[key] }' file.txt
Coding Round
94. How to print lines where a field starts with a capital letter?

Use $2 ~ /^[A-Z]/ as the pattern.

AWK
awk '{ if ($2 ~ /^[A-Z]/) print }' file.txt
Coding Round
95. How to convert a CSV file to pipe‑delimited?

Set FS="," and OFS="|", then $1=$1 to rebuild the record.

AWK
awk 'BEGIN { FS=","; OFS=" | " } { $1=$1; print }' file.csv
Coding Round
96. How to find all lines containing the word "error" and print line number?

Check each field for "error" and print NR and the line.

AWK
awk '{ for (i=1; i<=NF; i++) if ($i == "error") print NR, $0 }' file.txt
Coding Round
97. How to remove duplicate consecutive lines?

Compare each line with the previous one and print only if different.

AWK
awk '{ if (NR > 1 && $0 != prev) print $0; prev = $0 }' file.txt
Coding Round
98. How to extract all numbers from a file?

Iterate fields and print those that match a numeric regex.

AWK
awk '{ if ($1 ~ /[0-9]+/) print $1 }' file.txt
Coding Round
99. How to print the current filename with each line?

Use the built‑in variable FILENAME.

AWK
awk 'BEGIN { print "File:", ARGV[1] } { print }' file.txt
Coding Round
100. How to find the maximum value in a column?

Track the maximum in a variable and print it in END block.

AWK
awk '{ if ($1 > max) max = $1 } END { print "Max:", max }' file.txt