Download presentation
Presentation is loading. Please wait.
1
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
2
Session Objectives Decision making using if for loop while loop
3
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.
4
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 }
5
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.
6
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/ )
7
Contd.. if-else structure:
The if-else structure in awk is similar to C if-else. Example: if ( $6 < 6000 ) da = 0.25*$6 else da = 1000
8
Contd.. The above if-else can be replaced by if construct with a compact conditional structure as: $6 < ? da = 0.25*$6 : da = 1000
9
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++)
10
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.
11
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.
12
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.
13
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
14
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] >}’
15
Contd.. Output: LOGNAME = ISEDEPT MAIL=/var/mail/ISEDEPT
PATH=/usr/bin::/usr/local/bin::/usr/bin TERM=xterm HOME=/home/ISEDEPT SHELL=/bin/bash
16
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
17
Contd.. Output: manager 4 chairman 1 executive 2 director 3 Note:
Here the employee databases is break up and grouped on their designation.
18
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]).
19
LOOPING with while The while loop like for loop repeatedly iterates the loop until the command succeeds. Example: k=0 while (k < (55 – length($0))/2) { printf “%s”,“ ” k++ } print $0
20
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.
21
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.