You are on page 1of 23

Awk Programming

Awk- Pattern scanning and processing


language

Awk command is named after by its


creators :Aho,Weinberger and Kernigham
Combines pattern matching,
comparison,numerical operations.

The Awk text-processing language is


useful for such tasks as:

Awk has two faces: it is a utility for performing simple


text-processing tasks, and it is a programming
language for performing complex text-processing
tasks

Tallying information from text files and creating


reports from the results.

Translating files from one format to another.

Creating small databases.

Performing mathematical operations on files of


numeric data

Awk Syntax
Syntax: awk awk-command fielname
awk /anne/ {print $0} emp
awk {print $1,$3,$5} emp
awk /anne/ {print $0}
/jack/ {print $0} emp

Using command file

It is more convenient to place commands in


a file and have awk get its commands from
the file
Can be done using f option
$ awk f awkss1 emp

Field and Records

Awk has builtin variables dealing with


fields and records
NR record number
NF number of fields
RS record separator (new line character)
FS field separator which is a space or
tab,but can be reset to another value
By using F option
awk F: {print $1,$2} /etc/passwd

Pattern Matching Metacharacters for


awk
\

Match following character literally

Beginning of a line

$
.

End of the line


Any one character

[list] Any one character in list


[^list] Any one character not in list
*

Zero or more occurrences of the


preceding character

Regular Expressions and Fields


awk F : $2 ~/anne/ {print $0}
emp
awk $4 !~/sales/ {print $0} emp
awk $2 !~/^James$/ {print $0}
emp

Relational Expressions
operator

Meaning

<
>
==

Equal to

!=

Not equal to

>=
<=

Relational operators

$ awk $4>=5000 emp


Should list all records whose salaries
are more than 5k
$ awk $2==anne
$ awk $3 >= 4 * $2 salesrep

Logical operators
Operator

Meaning

&&

And

||

Or

not

awk $4>5000 && $3==mrktg emp

Print Redirection

{print $1>>col1; print $2>>col2;


print $3 >>col3 }

Special Patterns Begin and


End

Begin is used to label actions to be


done before any records are read .
BEGIN {RS=; FS=/}
End label is used to identify actions
that are done after all the input has
been read

Variables

awk F: $3==sales || $3==accts


{ctr=ctr+1}
END {print ctr, emps in sales and accts}
To sum all the salaries
{sum+=$4}
END{ print sum}

Eg : salary slip

awk F : {hra=$4*0.25;da=$4*0.5;print
$1,$4,hra,da} emp

This program prints the total


number of bytes used by files

ls -lg files | awk '{ x += $5 } ; END {


print "total bytes: " x }'

Program prints a sorted list of


the login names of all users

awk -F: '{ print $1 }' /etc/passwd |


sort

Program counts lines in a file

awk 'END { print NR }' data

Program to print the even numbered


lines in the data file. If you were to use
the expression `NR % 2 == 1' instead, it
would print the odd number lines.
awk 'NR % 2' data

Find the average file size

ll | awk 'NR!=1 {s+=$5} END {print


"Average: " s/(NR-1)}'

Useful awk programs


Program prints the length of the longest
input line :

awk '{ if (length($0) > max) max =


length($0) }
END { print max }' data
Program prints the length of the longest
input line:

awk 'length($0) > 80' data

Useful awk prgs

expand data | awk '{ if (x < length()) x =


length() }
END { print "maximum line length is " x }'

This program prints the length of the longest line


in `data'. The input is processed by the expand
program to change tabs into spaces, so the
widths compared are actually the right-margin
columns.

Useful awk prgs

awk 'NF > 0' data

Program prints every line that has at


least one field. This is an easy way to
delete blank lines from a file (or rather,
to create a new file similar to the old file
but from which the blank lines have been
deleted).

Program prints the total number of


kilobytes used by files
ls -lg files | awk '{ x += $5 }
END { print "total K-bytes: " (x +
1023)/1024 }'

You might also like