Awk- An Advanced Filter
Download
1 / 21

awk- An Advanced Filter - PowerPoint PPT Presentation


  • 49 Views
  • Uploaded on

awk- An Advanced Filter. by Prof. Shylaja S S

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' awk- An Advanced Filter' - quon-bell


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Awk an advanced filter

awk- An Advanced Filter

byProf. Shylaja S S

Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of Technology, Bangalore-560085 shylaja.sharath@pes.edu


Awk an advanced filter

Session Objectives

  • Decision making using if

  • for loop

  • while loop


Control flow the if statement

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

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 }


Contd1

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.


Contd2

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/ )


Contd3

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


Contd4

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

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++)


Contd5

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.


Contd6

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.


Contd7

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.


Contd8

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


Contd9

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]

  • >}’


Contd10

Contd..

Output:

LOGNAME = ISEDEPT

MAIL=/var/mail/ISEDEPT

PATH=/usr/bin::/usr/local/bin::/usr/bin

TERM=xterm

HOME=/home/ISEDEPT

SHELL=/bin/bash


Contd11

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


Contd12

Contd..

  • Output:

  • manager 4

  • chairman 1

  • executive 2

  • director 3

  • Note:

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


Contd13

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

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


Contd14

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.


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