awk- An Advanced Filter
description
Transcript of 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]
Session Objectives
• Decision making using if
• for loop
• while loop
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.
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 }
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.
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/ )
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
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
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++)
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.
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.
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.
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
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]>}’
Contd.. Output:
LOGNAME = ISEDEPT MAIL=/var/mail/ISEDEPTPATH=/usr/bin::/usr/local/bin::/usr/binTERM=xtermHOME=/home/ISEDEPTSHELL=/bin/bash
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
Contd.. Output:
manager 4chairman 1executive 2director 3
Note:
• Here the employee databases is break up and grouped on their designation.
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]).
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
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.
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