awk for Text Processing: Patterns That Replace Small Scripts
awk looks cryptic until you learn the pattern-action model. Once it clicks, it replaces dozens of small scripts. Here is how I use it. awk is the tool I reach for when a pipeline of grep, cut, and sort is a full Python script is overkill. It processes text files as records and fields, applies patterns to each record, and runs actions on matching records. The pattern-action model felt cryptic until I spent an afternoon with it, and then it clicked. Now I use awk for log analysis, column extraction, and conditional reformatting several times a week. The Record and Field Model awk reads input line by line. Each line is a record, and each record is split into fields separated by whitespace by default. The fields are available as $1 , $2 , and so on. $0 is the whole line. NF is the number of fields in the current record. # Print the second column of each line awk '{print $2}' data.txt # Print the last column awk '{print $NF}' data.txt This is the simplest awk usage, equivalent to cut -f2 but more flexible because awk handles variable whitespace without configuration. I…