You are on page 1of 84

awk An Advanced Filter [A Scripting Language]

About AWK

1) AWK is a report formatting tool 2) Named after Aho, Weinberger and Kernighan 3) AWK operates at the field level 4) AWK can easily access, transform and format individual fields in a line. 5) AWK has C programming language type constructs, variables and several built-in functions.

SIMPLE awk Filtering


Syntax
awk options selection_criteria {action} files(s)

Typical usage: Select the employees from sales dept of emp.lst

Here, selection_criteria section (/sales/) selects lines that are processed in the action section ({print})

Remember
Either of the selection_criteria and action is optional, but not both and they must be enclosed within a pair of single quotes. The print statement when used without any field specifiers, prints the entire line. Since printing is the default action of the awk, it can be omitted.

Execute these in your machine

awk '/director/' employee.lst awk '/director/ { print}' employee.lst awk '/director/ {print $0 }' employee.lst

Output

awk also supports regular expressions awk -F "|" '/[aA]g*[ar][ar]wal/' employee.lst

Check your understanding


True / False

awk is not just a command, but a programming language too.

TRUE

Check your understanding True / False

An awk program must have either the selection criteria or the action or both, but within single quotes.

True

Splitting a line into fields

awk uses a special parameter $0, to indicate entire line.


Individual fields can be accessed with $1, $2, $3 etc...

Remember Since $1, $2, $3 etc have got special meaning to the shell, we use single-quotes to protect the interpretation by the shell. Awk uses whitespace as the default delimeter

Write a simple awk to print name, designation, department and alary of all the admin dept. people.

awk has a built-in variable, NR, to specify line number. Write a awk script to select lines 3 to 6 and print line number, name, designation and salary.

Printf : FORMATTING OUTPUT

Redirecting Standard Output

The Comparison Operators Write an awk script to print the name, designation and salary of all directors and chairman.

For negating the above condition, we use != Write an awk script to print the name, designation and salary of all employees who are neither directors nor chairman.

The Regular Expression Operators


~ and !~ An awk to bring records of both aggarwal and agrawal

Write an awk script to print the name, designation and salary of all employees who are neither directors nor chairman.

Check your understanding


True/False To match a string embedded in a field, we have to use ~ instead of ==, and to negate use !~ instead of !=

True

Number Comparison Write an awk to print name, designation and salary of all those people whose pay exceeds 6000.

Write an awk program to locate those who are born in 1950.

Check your understanding


True / False To match a string at the beginning of the field, precede the search by a $. Similarly use a ^ for matching a pattern at the end of a field.

False

Number Processing
Write an awk script to print salary slip of all directors, assume that the 6th field gives basic salary, compute DA @ 40% of basic pay, HRA @ 15% of basic pay.

Variables
1) No type declarations needed 2) By default initialized to zero or null string Write an awk script to print serial numbers for all those directors drawing salary exceeding 6500.

Check your understanding True / False In awk, for user defined variables no type declarations or initial values are required.

The -f option. Storing awk programs in a file AWK programs can be stored in a file with .awk extension.

Note

awk uses single quotes only when the program is specified in the command line or when the entire awk command line is held in a shell script.

The BEGIN and END sections awk provides BEGIN section to allow us to have a header before printing something and END section to allow us to have a footer, print something after the processing is over.

The BEGIN and END sections are optional.


Syntax BEGIN { action} END {action}

Write an awk script to print a suitable header at the beginning and average salary at the end of all employees with line numbers.

Write an awk script to locate those lines not having 3 fields from the file empx.lst

Arrays awk handles one-dimensional array.

The index for an array can be anything.


An array is considered to be declared the moment it is used and is initialised to zero unless initialized explicitly.

Write an awk script, to print the total of the basic pay, da , hra and gross of sales and marketing people [use arrays]

Functions

awk has several built-in functions for performing both arithmetic and string operations.

int(x) - Usage

int(x) input using echo

sqrt(x) - usage

length - usage

length(x) - usage

index(s1,s2) - usage

substr(stg,m,n) -usage

Write an awk script using substr function, to select those born between 1946 and 1951:

split(stg,arr,ch) - usage

split(stg,arr,ch) breaks up a string stg on the delimeter ch and stores the fields in an arrar arr[]. Write an awk to covert the date field from the current DD/MM/YY to the format YYYY-MM-DD

system(cmd) - usage

CONTROL FLOW The if statement


awk has practically all the features of a modern programming langauge like if, for, while They all execute a body of statements depending on the success of the control command.

if - usage

Write an awk to print the director pay slip, assume that 6th field signifies basic salary; if the basic pay is less than 6000, compute DA=25% of basic and 1000 otherwise.

awk 'BEGIN {FS="|"; system("clear"); printf "\n\t Director Pay Slip\n";} /director/ { if ($6 > 6000) {DA=$6*0.25} else DA=1000; printf "%5s %d %d\n",$2,$6,DA}' employee.lst

Using compact conditional strcuture

awk 'BEGIN {FS="|"; system("clear"); printf "\n\t Director Pay Slip\n";} /director/ {$6 > 6000 ? DA=$6*0.25 : DA=1000; printf "%5s %d %d\n",$2,$6,DA}' employee.lst

Check your understanding


True / Fasle There is no endif or if terminator for the if statement used in awk. awk uses the { and } to define a block of code.

LOOPING WITH for


form-1

for(k=1;k<=9;k++)
Write an awk to center the text given to awk by echo using for loop.

Form-2 for ( k in array)

{
commands }

Write an awk to display a count of employees, grouped according to designation(3rd field). Hint : [$3 can be used as a subscript of an array]

Note

We had implemented the same logic using tr, cut,sort and uniq commands.

LOOPING WITH while

Implementing centring text logic using while loop

Thank YOU

You might also like