Professional Documents
Culture Documents
Swiss army knife of UNIX system toolkit. The AWK command made a late entry into the
UNIX system in 1977 to augment the toolkit with suitable report formatting capabilities.
Anmed after its authors, Aho, Weinberger, and Kernigham, AWK until the advent of
PERL, was the most powerful utility for text manipulation. AWK appears as GAWK
(GNU AWK) in Linux. AWK was first implemented inSVR3.1. AWK works with
database files only. Unlike other filters, AWK operates at the field level. It can easily
access, transform and format individual records. It also accepts regular expression for
pattern matching, has C-type programming construct, varibles and several built-in
functions.
TIP: An AWK program must have either pattern or action, or both, but within
single quotes.
The following three forms could be considered equivalent:-
$ awk ‘/director/’ emp.lst #Printing is default action.
$ awk ‘/director/ { print }’ emp.lst White space permitted.
$ awk ‘/director/ { print $0 }’ emp.lst $0 is the complete line.
TIP: BEGIN and END pattern without any action makes no sense. Always start the
opening bracein the same line the section(BEGIN & END) begins.
Display the total salary of all employees.
$ awk-f “|” ‘BEGIN{total=0}
{total= total + $6}
END{print “Total salary is”, total}’emp.lst
TIP: No type declaration or initial values are required for user defined variables
used in an AWK program. AWK identifies their type and initailise them to zero or
null string.
BUILT_IN VARIABLES
AWK has several built-in variables. They are all assigned automatically, though it is also
possible for a user to reassign them.
VARIABLES FUNCTION
NR Cumulative number of records read.
FS Input field seperator
OFS Output field seperator.
NF Number of fields in current record.
RS Record seperator.
FILENAME Current input file.
For identification of fields, AWK uses a contiguous string of spaces as the default field
delimiter. FS redefines the field seperator, when used at all, it must occur in the BEGIN
section so that the body of the program knows its value before it starts processing.
BEGIN ( FS = “|”)
This is alternave to the – F option of the command which does the same thing.
The OFS variable: when you use the print statement with comma seperated arguments,
each argument was seperated from the others by a space. This is Awk’s default output
field seperator, and can be reassigned using the variable OFS in the begin section.
BEGIN { OFS = “~” }
When you reassign this variable with a ~, Awk will use this character for delimiting the
print arguments.
FILENAME stores the name of the current file being processed.
TIP: Awk is the only filter that uses white space as the default delimiter instead of a
single space.
POSITIONAL PARAMETERS
AWK also uses positional parameters like the shell, except that they have to be placed in
single quotes. This would enable AWK to distinguish between a positional parameter and
a field identifier. The entire AWK command should now be stored in a shell script, and
the parameter supplied as an argument to the script.
Awk-F “|” ‘BEGIN{print “Employee”}
($6 > 7500) {
count++;
print $2, $3, $4 }
END{ “the average is”,count }
So instead of having ($6 > 7500) as th eline specifier, you should generalise it as
$6 > ’$1’. If you now have the script empabs.awk containing the entire awk command,
you can invoke the command with an argument , say 8000:
empabs.awk 8000
MAJOR DRAWBACK OF AWK
It doesn’t take input from keyboard. To take input from terminal, AWK uses
getline which has got a diverse syntax:
getline <variable> <”</dev/tty”