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

In other words, it gets input and shows it in a different way, making it simple to grab only the desired information.

Actually, AWK is much more complex 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 means the Number of Field, and can represent the last field:

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

Setting another delimiter character instead of 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}'

Executing command before and after:

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

Adding RegEx patterns (conditions) to the 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;
}