The Unix "awk" command

iKnow-IT banner

AWK (Aho - Weinberger - Kernighan "The programmable filter")

$ cat /etc/passwd
root:!:0:0:Super User:/:/bin/ksh ….
Jasper:!:200:1:System Administor:/home/jasper:/bin/ksh ….
$ cat /etc/passwd | awk ’/jasper/ { print $0 }’
Jasper:!:200:1:System Administor:/home/jasper:/bin/ksh
cat /etc/passwd |
awk ’ 
BEGIN { print “all user accounts with ksh and csh“ }
   /ksh/ { print $0 }
   /csh/ { print $0 }
END { print “These were …” } ’
$ ls -l | awk ’
BEGIN { print ”Custom directory list” }
{ b+=$5; print $9 ” \t” $5 ” \t bytes” }
END { print ”Total: ” b ” bytes ” } ’

Custom directory list
file1.dat              109   bytes
myawk.sh          302   bytes
myawk.out       2034   bytes
Total: 2445 bytes $

$ awk ’
BEGIN { FS=”:” ; print ” \t Account \t Name ” } 
{ print NR ”. \t” $1 ”\t” $5 }’ /etc/passwd

          Account       Name

1.      root               Super User
2.     daemon        System Daemon
..
..
16.     guest           Guest user

 $

$ ls -l

-rw-r--r-- 1   oracle   dba   123  Mar 09  19:32  init.ora
-rw-r--r-- 1   oracle   dba   231  Mar 09  19:48  inittest.ora
-rw-r--r-- 1   oracle   dba   95    Mar 06  14:20  test.sga

$ ls -l | awk ‘ 
{ if ( Biggest < $5 ) { Biggest = $5; Biggest_file = $9 } } 
END { print “Biggest file is “ Biggest_file “ “ Biggest “ bytes“ }’

Biggest file is inittest.ora 231bytes
$