Running awk and gawk


Awk is gawk on all distros I know.

awk '{printf "%3$s\t%2$s %1$s\n",$11,$10,$9}' <(ls -trl $(which awk))
/usr/bin/awk    -> gawk

Command line options

# set the FS variable to fs
  -F fs
  --field-separator fs
# read the awk program source
  -f source-file
  --file source-file
# set the variable var
  -v var=val
  --assign var=val
# print short version of GPL

From keyboard

Type Ctrl-d (the end-of-file character) to terminate.

awk -f /dev/tty
BEGIN { print "Don't Panic!" }
Don't Panic!

Command line arguments

ARGV command-line arguments
ARGC number of command-line arguments
ARGIND index in ARGV
awk 'BEGIN{
    for (i = 0; i < ARGC; i++)
        print i, ARGV[i]
    }' inventory-shipped mail-list toto
0 awk
1 inventory-shipped
2 mail-list
3 toto

Process stdin and files

ls -trl | awk '{print $0}' inventory-shipped - mail-list

Use “-” to name standard input.

Environment variable

AWKPATH path for each program file
AWKLIBPATH path for loadable extensions

Including files

cat test1
print "This is script test1."
awk -f test1
This is script test1.
cat test2
@include "test1"
print "This is script test2."
awk -f test2
This is script test1.
This is script test2.

Load extension

awk '@load "ordchr"; BEGIN {print chr(65)}'