Frequently asked questions and answers of AWK in Artificial Intelligence and Machine Learning of Computer Science to enhance your skills, knowledge on the selected topic. We have compiled the best AWK Interview question and answer, trivia quiz, mcq questions, viva question, quizzes to prepare. Download AWK FAQs in PDF form online for academic course, jobs preparations and for certification exams .
Intervew Quizz is an online portal with frequently asked interview, viva and trivia questions and answers on various subjects, topics of kids, school, engineering students, medical aspirants, business management academics and software professionals.
Question-1. What is AWK?
Answer-1: AWK is a programming language designed for text processing and data extraction, often used in Unix/Linux environments.
Question-2. What are the key features of AWK?
Answer-2: AWK supports pattern matching, text processing, associative arrays, and built-in string functions.
Question-3. What is the basic syntax of an AWK command?
Answer-3: awk 'pattern {action}' filename where pattern specifies when to execute the action.
Question-4. What are records in AWK?
Answer-4: Records are the units of input processing, typically one line of text.
Question-5. What are fields in AWK?
Answer-5: Fields are parts of a record separated by a delimiter, accessed as $1, $2, etc.
Question-6. How do you specify a field separator in AWK?
Answer-6: Use the -F option or set FS inside the script, e.g., awk -F, '{print $1}' file.
Question-7. What is the default field separator in AWK?
Answer-7: The default field separator is whitespace (spaces and tabs).
Question-8. How do you print all fields in AWK?
Answer-8: Use the special variable $0, e.g., awk '{print $0}' file.
Question-9. What is the BEGIN block in AWK?
Answer-9: The BEGIN block executes code before processing any records.
Question-10. What is the END block in AWK?
Answer-10: The END block executes code after all records are processed.
Question-11. How do you print the second field of a file using AWK?
Answer-11: Use awk '{print $2}' file.
Question-12. How do you process only lines that match a pattern in AWK?
Answer-12: Use the pattern before the action, e.g., awk '/pattern/ {print $0}' file.
Question-13. How do you count the number of lines in a file using AWK?
Answer-13: Use awk 'END {print NR}' file.
Question-14. What does NR represent in AWK?
Answer-14: NR is a built-in variable that holds the current record number.
Question-15. What does NF represent in AWK?
Answer-15: NF is a built-in variable that holds the number of fields in the current record.
Question-16. How do you use AWK to find lines longer than 50 characters?
Answer-16: Use awk 'length($0) > 50' file.
Question-17. How do you sum the values in the second column of a file?
Answer-17: Use awk '{sum += $2} END {print sum}' file.
Question-18. What is an associative array in AWK?
Answer-18: An associative array stores key-value pairs and allows string indexing.
Question-19. How do you define an associative array in AWK?
Answer-19: Use array[key] = value, e.g., count["apple"] = 5.
Question-20. How do you iterate over an array in AWK?
Answer-20: Use a for loop, e.g., for (key in array) {print key, array[key]}.
Question-21. What is the purpose of the split() function in AWK?
Answer-21: The split() function divides a string into an array based on a delimiter.
Question-22. How do you split a string into an array in AWK?
Answer-22: Use split(string, array, delimiter), e.g., split("a,b,c", arr, ",").
Question-23. What is the gsub() function in AWK?
Answer-23: gsub() globally replaces all occurrences of a pattern in a string with a replacement.
Question-24. How do you replace "foo" with "bar" in AWK?
Answer-24: Use gsub(/foo/, "bar", $0) to modify the current record.
Question-25. How do you use regular expressions in AWK?
Answer-25: Use patterns enclosed in slashes, e.g., /pattern/ {action}.
Question-26. What does ^ and $ mean in AWK regular expressions?
Answer-26: ^ matches the start of a line, and $ matches the end of a line.
Question-27. How do you extract the last field of each line in AWK?
Answer-27: Use awk '{print $NF}' file.
Question-28. How do you print the first and last field of a record in AWK?
Answer-28: Use awk '{print $1, $NF}' file.
Question-29. How do you calculate the average of a column in AWK?
Answer-29: Use awk '{sum += $2} END {print sum / NR}' file.
Question-30. How do you skip the first line in AWK?
Answer-30: Use NR > 1 as the condition, e.g., awk 'NR > 1 {print $0}' file.
Question-31. What is the purpose of next in AWK?
Answer-31: The next statement skips the rest of the current iteration and moves to the next record.
Question-32. How do you ignore case in AWK pattern matching?
Answer-32: Set IGNORECASE=1 or use tolower()/toupper().
Question-33. How do you sort lines in AWK?
Answer-33: Use an array to store lines and sort them manually with a loop.
Question-34. How do you use AWK with pipes?
Answer-34: Use awk in a pipeline, e.g., `cat file
Question-35. How do you match multiple patterns in AWK?
Answer-35: Combine patterns with `
Question-36. How do you append output to a file in AWK?
Answer-36: Use >> redirection, e.g., awk '{print $1 >> "output.txt"}'.
Question-37. What is the purpose of printf in AWK?
Answer-37: printf formats and prints output, similar to C's printf.
Question-38. How do you format output in AWK?
Answer-38: Use printf, e.g., printf("%.2f\n", $1).
Question-39. How do you check if a field is empty in AWK?
Answer-39: Use a condition like $1 == "".
Question-40. How do you print the number of fields in each line?
Answer-40: Use awk '{print NF}' file.
Question-41. How do you use comments in AWK?
Answer-41: Use # to add comments, e.g., # This is a comment.
Question-42. How do you concatenate strings in AWK?
Answer-42: Use a space or "", e.g., print $1 $2.
Question-43. What is the substr() function in AWK?
Answer-43: The substr() function extracts a substring from a string.
Question-44. How do you extract the first 5 characters of a string in AWK?
Answer-44: Use substr(string, 1, 5).
Question-45. How do you check for numeric values in AWK?
Answer-45: Use a condition like $1 ~ /^[0-9]+$/.
Question-46. How do you execute shell commands in AWK?
Answer-46: Use system(), e.g., system("ls -l").
Question-47. How do you find unique values in a column using AWK?
Answer-47: Use an associative array to track seen values, e.g., if (!seen[$1]++).
Question-48. How do you convert a string to lowercase in AWK?
Answer-48: Use tolower(string).
Question-49. How do you find the maximum value in a column using AWK?
Answer-49: Use if ($2 > max) max = $2 END {print max}.
Question-50. How do you run an AWK script from a file?
Answer-50: Use awk -f script.awk inputfile.
Frequently Asked Question and Answer on AWK
AWK Interview Questions and Answers in PDF form Online
AWK Questions with Answers
AWK Trivia MCQ Quiz