AWK is a domain-specific language designed for text processing, typically used as a data extraction and reporting tool.

In other words, it takes input and displays it differently, making it easy to extract only the information you need.

AWK is much more than just grep, sed, or similar Linux commands. It is a complete scripting language.


Printing the desired columns:

awk '{print}' file.txt
awk '{print $1}' file.txt
who | awk '{print $1,$4}'

NF stands for Number of Fields, and can be used to represent the last field:

awk '{print $NF}' file.txt
awk '{print $(NF-1)}' file.txt

Setting a custom delimiter instead of a space:

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

Defining the output field separator:

date | awk 'OFS="/" {print $7,$2,$3}'
date | awk 'OFS="-" {print $7,$2,$3}'

Running commands before and after processing:

who | awk 'BEGIN {print "Connected users:"} {print $1,$4} END {print "End of the list."}'

Adding RegEx patterns (conditions) to filter data:

awk -F: '$3 >= 1000 {print $1,$3}' /etc/passwd
awk '/UUID/ {print $1}' /etc/fstab

A short list of built-in functions:

  • Numeric
    • atan2(y, x)
    • cos(x)
    • exp(x)
    • int(x)
    • log(x)
    • rand()
    • sin(x)
    • sqrt(x)
    • srand([x])
  • String
    • asort(source [, dest [, how ] ])
    • asorti(source [, dest [, how ] ])
    • gensub(regexp, replacement, how [, target])
    • gsub(regexp, replacement [, target])
    • index(in, find)
    • length([string])
    • match(string, regexp [, array])
    • patsplit(string, array [, fieldpat [, seps ] ])
    • split(string, array [, fieldsep [, seps ] ])
    • sprintf(format, expression1, …)
    • strtonum(str)
    • sub(regexp, replacement [, target])
    • substr(string, start [, length ])
    • tolower(string)
    • toupper(string)
  • Input/output
    • close(filename [, how])
    • fflush([filename])
    • system(command)
  • Time
    • mktime(datespec [, utc-flag ])
    • strftime([format [, timestamp [, utc-flag] ] ])
    • systime()
  • Bits
    • and(v1, v2 [, …])
    • compl(val)
    • lshift(val, count)
    • or(v1, v2 [, …])
    • rshift(val, count)
    • xor(v1, v2 [, …])
  • Type
    • isarray(x)
    • typeof(x)
awk 'BEGIN {print function(arg1, "arg2")}'
awk 'length($0) > 80'
awk '{ if (length($0) > max) max = length($0) } END { print max }'

Scripting:

nano cmd.awk
#!/bin/awk -f
BEGIN { print "Hello!" }
chmod +x cmd.awk
./cmd.awk

Examples:

#!/bin/awk -f
BEGIN { n=1
while ( n < 5 )
 {
  print n;
  n++;
  }
}
#!/bin/awk -f
BEGIN {
for (i=1; i <= 10; i++) {
  printf "The square of ", i, " is ", i*i;
  }
exit;
}
#!/bin/awk -f
BEGIN {
  print "Start";
}
{
  print "Perform";
}
END {
  print "Complete"
}
#!/bin/awk -f
BEGIN {
    lines=0;
    total=0;
}
{
    lines++;
    total+=1;
}