awk

15 commands

15 shown

Print specific column Print the 3rd whitespace-delimited field awk '{print $3}' file.txt #awk#linux Custom field separator Use colon as field separator, print user and UID awk -F: '{print $1, $3}' /etc/passwd #awk#linux Filter rows by pattern Print line number and content for lines matching ERROR awk '/ERROR/ {print NR": "$0}' app.log #awk#linux#debug Sum a column Accumulate a numeric column and print total awk '{sum += $4} END {print sum}' access.log #awk#linux Print lines between two patterns Range pattern: include lines from START to END inclusive awk '/START/,/END/' file.txt #awk#linux Skip header line Process all lines except the first (header) awk 'NR>1 {print $1, $2}' data.csv #awk#linux Count occurrences per key Frequency count of the first field (e.g. IP addresses) awk '{count[$1]++} END {for (k in count) print count[k], k}' access.log | sort -rn #awk#linux#network Print last field NF holds the number of fields; $NF is the last field awk '{print $NF}' file.txt #awk#linux Multi-condition filter Print lines where field 2 > 500 AND field 5 is POST awk '$2 > 500 && $5 == "POST"' access.log #awk#linux Reformat output Aligned printf formatting with fixed column widths awk '{printf "%-20s %5d\n", $1, $2}' data.txt #awk#linux Run awk script from file Load awk program from an external script file awk -f transform.awk input.txt #awk#linux In-place-style transform with awk Global substitution and write back to same file awk '{gsub(/foo/, "bar"); print}' input.txt > tmp && mv tmp input.txt #awk#linux Process CSV with quotes (gawk) Parse quoted CSV fields correctly with FPAT in gawk gawk -v FPAT='([^,]+)|("[^"]+")' '{print $2}' data.csv #awk#linux BEGIN/END blocks Run code before and after processing all lines awk 'BEGIN{print "Start"} {lines++} END{print lines" lines processed"}' file.txt #awk#linux Two-file join on key Left-join data.txt with lookup.txt on first field awk 'NR==FNR{a[$1]=$2; next} $1 in a {print $0, a[$1]}' lookup.txt data.txt #awk#linux
no commands match