awk- An Advanced Filter

21
awk- An Advanced Filter by Prof. Shylaja S S Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of

description

awk- An Advanced Filter. by Prof. Shylaja S S - PowerPoint PPT Presentation

Transcript of awk- An Advanced Filter

Page 1: awk- An Advanced Filter

awk- An Advanced Filter

by Prof. Shylaja S S Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of Technology, Bangalore-560085 [email protected]

Page 2: awk- An Advanced Filter

Session Objectives

• Decision making using if

• for loop

• while loop

Page 3: awk- An Advanced Filter

Control Flow- The If Statement:

• awk has conditional structures (the if statement) and loops (while or for).

•These control structures execute the body of statements depending on the success or failure of the control command.

• A control command is a condition that is specified in the first line of the construct.

Page 4: awk- An Advanced Filter

Contd.. • The if statement can be used when the && and || are found to be inadequate for certain tasks.

Syntax:

If (condition is true) { Statement } else { Statement }

Page 5: awk- An Advanced Filter

Contd.. • For example, to select lines where the basic pay exceeds 8000, the selection criteria is:

$4 > 8000 • An alternative form of this requires the if statement:

awk –F “|” ‘{ if ($4 > 8000) printf ……….

• if can be used with the comparison operators.

Page 6: awk- An Advanced Filter

Contd.. • It can be used with special symbols ~ and !~ to match a regular expression.

• When used in combination with the logical operators || and &&, awk programming becomes quite easy and powerful.

Examples: if ( NR > = 2 && NR <= 5 )if ( $2 == “manager” || $2 == “dm” )if ( $4 ~ /^d.g.m/ )

if ( $2 !~ / [aA]gg?[ar]+wal/ )

Page 7: awk- An Advanced Filter

Contd.. if-else structure:

The if-else structure in awk is similar to C if-else.

Example:

if ( $6 < 6000 )da = 0.25*$6

elseda = 1000

Page 8: awk- An Advanced Filter

Contd.. • The above if-else can be replaced by if construct with a compact conditional structure as: $6 < 6000 ? da = 0.25*$6 : da = 1000

Page 9: awk- An Advanced Filter

Looping with for• awk supports two looping constructs namely for and while. • The for and while execute the loop body as long as the control command returns a true value. • The for loop has two forms. • First is a simple for that resembles its C counterpart. • Example: for (count=0; count<10; count++)

Page 10: awk- An Advanced Filter

Contd..This simple form consists of three components: Initialization, which initializes the value of k, Conditional Expression, which checks the condition in every iteration Increment, while the sets the increment used for every iteration.

Note:for is useful for centering text.

Page 11: awk- An Advanced Filter

Contd..Example: $ echo “>Salary Statement \n for\n this Month” |>awk ‘ { for (i=1 ; i < (55 –length($0)) /2 ; i++)>printf “%s”,” “>printf $0}’emp.lst

The above examples uses awk for loop with echo in a pipeline to centre the text.

Page 12: awk- An Advanced Filter

Contd..Output:Salary Statement

for this Month

• Here the for loop uses the first printf statement to print the required number of spaces (page width assumed to be 55 ).

• The line is then printed with the second printf statement, which falls outside the loop.

Page 13: awk- An Advanced Filter

Contd.. Using for with an Associative Array:

• The second form of the for loop uses the associative feature of awk’s arrays.

• This form is similar to that of perl.

• The loop selects each index of an array.

Syntax:for ( k in array )

commamds

Page 14: awk- An Advanced Filter

Contd..• Here, k is the subscript of the array arr.• Since k can be strings, all environment variables can also be printed.

Example:

$ awk ‘BEGIN {>for ( key in ENVIRON )>print key “=” ENVIRON [key]>}’

Page 15: awk- An Advanced Filter

Contd.. Output:

LOGNAME = ISEDEPT MAIL=/var/mail/ISEDEPTPATH=/usr/bin::/usr/local/bin::/usr/binTERM=xtermHOME=/home/ISEDEPTSHELL=/bin/bash

Page 16: awk- An Advanced Filter

Contd.. • Since the index is a string, any field can be used as index. • Elements of the array can be used as counters.

Example:

$awk –F ’|’ ‘{ count[$3]++ }>END { for ( desig in kount)>print desig, kount[desig] }’ emp.lst

Page 17: awk- An Advanced Filter

Contd.. Output:

manager 4chairman 1executive 2director 3

Note:

• Here the employee databases is break up and grouped on their designation.

Page 18: awk- An Advanced Filter

Contd.. • The array count[] takes as its subscript non-numeric values manger, chairman, executive, director.

• for is invoked in the END section to print the subscript (desg) and the number of occurrence of the subscript (count[desg]).

Page 19: awk- An Advanced Filter

LOOPING with while• The while loop like for loop repeatedly iterates the loop until the command succeeds.

• Example:

k=0while (k < (55 – length($0))/2) {printf “%s”,“ ”k++}print $0

Page 20: awk- An Advanced Filter

Contd.. • Here the while loop prints a space and increments the value of k with every iteration.

• The condition (k < (55 – length($0))/2) is tested at the beginning of every iteration, and the loop body only if the test succeeds.

Page 21: awk- An Advanced Filter

Conclusion

In this session we saw topics of awk like:.

•Use of if decision making structure helpful in writing awk programs.

• Looping constructs like for and while.

•Two varieties of using for