AWK Commands

AWK pattern scanning and text processing commands.

Basic Output

awk '{print}' file.txt

Print all lines from file

awk '{print $0}' file.txt

Print entire line (same as {print})

awk '{print $1}' file.txt

Print first field of each line

awk '{print $NF}' file.txt

Print last field of each line

awk '{print $1, $3}' file.txt

Print first and third fields

awk '{print $1 ":" $2}' file.txt

Print fields with custom separator

Field Separators

awk -F"," '{print $1}' file.csv

Use comma as field separator

awk -F":" '{print $1}' /etc/passwd

Use colon as field separator

awk -F"\t" '{print $1}' file.tsv

Use tab as field separator

awk 'BEGIN {FS=","} {print $1}' file.csv

Set field separator in BEGIN block

awk 'BEGIN {OFS=","} {print $1, $2}' file.txt

Set output field separator

Pattern Matching

awk '/pattern/ {print}' file.txt

Print lines matching pattern

awk '!/pattern/ {print}' file.txt

Print lines not matching pattern

awk '/start/,/end/ {print}' file.txt

Print lines between patterns

awk '$1 ~ /pattern/ {print}' file.txt

Print if first field matches pattern

awk '$1 !~ /pattern/ {print}' file.txt

Print if first field does not match

awk '/error|warning/ {print}' log.txt

Match multiple patterns with OR

Line Selection

awk 'NR==5 {print}' file.txt

Print 5th line

awk 'NR>=5 && NR<=10 {print}' file.txt

Print lines 5 through 10

awk 'NR%2==1 {print}' file.txt

Print odd numbered lines

awk 'NR%2==0 {print}' file.txt

Print even numbered lines

awk 'END {print NR}' file.txt

Print total number of lines

awk 'NR>1 {print}' file.txt

Skip first line (header)

Conditional Operations

awk '$3 > 100 {print}' file.txt

Print lines where 3rd field > 100

awk '$2 == "active" {print}' file.txt

Print lines where 2nd field equals "active"

awk 'length($0) > 80 {print}' file.txt

Print lines longer than 80 characters

awk '$1 != "" {print}' file.txt

Print lines where first field is not empty

awk 'NF > 3 {print}' file.txt

Print lines with more than 3 fields

awk '$5 >= 50 && $5 <= 100 {print}' file.txt

Print if 5th field between 50-100

Calculations

awk '{sum += $1} END {print sum}' file.txt

Sum values in first column

awk '{sum += $1; count++} END {print sum/count}' file.txt

Calculate average of first column

awk 'BEGIN {max=0} {if ($1>max) max=$1} END {print max}' file.txt

Find maximum value

awk '{total += $2} END {print total}' file.txt

Sum values in second column

awk '{print $1, $2*1.1}' file.txt

Multiply second field by 1.1

awk '{print NR, $0}' file.txt

Add line numbers to output

String Operations

awk '{print toupper($0)}' file.txt

Convert to uppercase

awk '{print tolower($0)}' file.txt

Convert to lowercase

awk '{print length($0)}' file.txt

Print length of each line

awk '{print substr($1, 1, 3)}' file.txt

Print first 3 characters of field 1

awk '{gsub(/old/, "new"); print}' file.txt

Replace all occurrences globally

awk '{sub(/old/, "new"); print}' file.txt

Replace first occurrence only

BEGIN & END Blocks

awk 'BEGIN {print "Header"} {print} END {print "Footer"}' file.txt

Add header and footer

awk 'BEGIN {FS=","; OFS="|"} {print $1, $2}' file.csv

Convert CSV to pipe-delimited

awk 'BEGIN {count=0} /pattern/ {count++} END {print count}' file.txt

Count matching lines

awk 'BEGIN {print "Name,Score"} {print $1","$2}' file.txt

Add CSV header

Advanced Techniques

awk '{a[$1]++} END {for (i in a) print i, a[i]}' file.txt

Count occurrences of each value in field 1

awk '!seen[$0]++' file.txt

Remove duplicate lines

awk '{print $2, $1}' file.txt

Swap first and second fields

awk 'NF {print $NF}' file.txt

Print last field of non-empty lines

awk '{for(i=1;i<=NF;i++) print $i}' file.txt

Print each field on separate line

awk '{s=""; for(i=NF;i>=1;i--) s=s $i " "; print s}' file.txt

Reverse field order

Working with Files

awk '{print > $1".txt"}' file.txt

Split file based on first field value

awk 'FNR==1 {print FILENAME}' *.txt

Print filename for each file

awk 'FNR==NR {a[$1]; next} $1 in a' file1 file2

Print lines from file2 where field 1 is in file1

awk '{print FILENAME, $0}' *.log

Prepend filename to each line