awk-a powerful utility slide1
Awk is a powerful language which is useful for various kinds of
file operations. Often a one or two line awk program will do
the same thing as a much longer program in a general purpose
higher level language.
In short, awk can be your good friend, but he may be hard to
get to know well.
awk operates on LINES comprised of FIELDS
Fields are separated by white-space or a specified field
delimiter.
awk-a powerful utility slide2
Sample invocations (two methods):
awk 'length > 72' (The stuff in quotes is a whole, short awk
program.)
awk -F: -f awkfilename (awkfilename contains an awk program,
separator is ':')
Note that these examples have no redirection but that
redirection is normally used. Thus the first example would
probably be used as:
cat whatever | awk 'length > 72' OR (better)
awk 'length > 72' < whatever
This last example shows why awk programs will normally be
enclosed in single quotes. (Else many special characters will
be interpreted by the shell.)
By the way, awk 'length > 72' will print all lines of length
more than 72 characters.
awk-a powerful utility slide3
Awk programs are of the following form:
pattern1 { list of actions-1 }
pattern2 { list of actions-2 }
etc
Patterns (Though called "patterns", some of these would be
better named as "conditions".)
Pattern Meaning
------- -------
/string/ "string" appears in this line.
NF==3 The number of fields on this line is 3.
NR==2 This is line number 2.
$1=="vaxfilename" The first field is "vaxfilename".
awk-a powerful utility slide4
Pattern Meaning
------- -------
BEGIN No lines read yet.
END All lines have been read.
NR==1,NR==5 One of first five lines.
NR>=1&&NR<=5 One of first five lines.
/start/,/stop/ From first line with "start" to first line
that follows which contains "stop".
Many others!!!
awk-a powerful utility slide5
Awk actions.
As our earliest example:
awk 'length>72' < file
illustrated, the action may be omitted; the default is print the line.
Also note that the pattern can be omitted; then the action will
be taken on every line.
Hence:
awk 'NR>=1' < file (Action omitted)
OR awk '{print}' < file (Pattern omitted)
both will print the file. (They function like: cat file.)
awk-a powerful utility slide6
Awk actions, Printing.
Action Comment
------ -------
{print NR, $0} NR is record number (line number).
$0 is whole line.
{print $2} Prints the second field.
{printf "\t%s\n", $0} Prints ,the line,. This is
a lot like the printf of C.
awk-a powerful utility slide7
Awk arithmetic
+ Fields are denoted by $1, $2 ... ,$NF
+ Fields may be used for string OR arithmetic purposes.
A simple example:
{ s = s + $1 }
END { print s }
This adds all the numbers in the first field and prints the
result.
awk-a powerful utility slide8
Built-in Functions (An incomplete list)
length(string)
split(string,array,separator) (Returns the number of fields)
index(string1,string2)
For example, if a file has lines such as:
Nov/17/1989 Wildenberg,Gerald ,
then the awkfile:
{split($1,date,"/"); split($2,name,","); print "Month", date[1] , "First name" , name[2] }
will produce:
Month Nov First name Gerald
awk-a powerful utility slide9
Finally, awk has control structures:
if-else, for, while.
Awk also has the following operators:
Operator Meaning
-------- -------
= += -= /= %= Replacement
|| or
&& and
! negation
> >= < <= == != relations: less than, etc.
+ - Addition, subtraction.
* / % % is remainder.
++ -- Increment, decrement.
nothing Concatenation
awk-a powerful utility slide10
EXAMPLES
Print login name and real name from paswd file.
awk '{print $1 " " $5}' < /etc/passwd
Print first two fields in opposite order:
{ print $2, $1 }
Add up first column, print sum and average:
{ s += $1 }
END { print "sum is", s, " average is", s/NR }
Print fields in reverse order:
{ for (i = NF; i > 0; --i) print $i }
Print all lines whose first field is different from previous
one:
$1 != prev { print; prev = $1 }
Remove extra spaces.
{L=""; for (i = 1; i <= NF; i++) L = L" "$i; print L }
Note use of concatenation.