🐧 Linux / Shell Scripting
Intermediate
How do you use awk for text processing?
Answer
awk is a pattern-scanning and processing language. It processes input record by record (default: line by line) and splits each into fields (default: whitespace-separated). $1, $2, etc. refer to fields; $0 is the whole line; NF is number of fields; NR is line number. Example: awk '{print $1, $3}' file prints fields 1 and 3. Filter: awk '/pattern/ {print $2}' file. Arithmetic: awk '{sum += $1} END {print sum}' numbers.txt. Custom delimiter: awk -F: '{print $1}' /etc/passwd prints usernames. BEGIN and END blocks run before/after all input. awk is excellent for structured data like CSVs and log files.