awk- An Advanced Filter

Slides:



Advertisements
Similar presentations
Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Advertisements

Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Computer Science 101 While Statement. Iteration: The While-Statement The syntax for the While- Statement is while : The syntax for the While- Statement.
Decision Making EE2372 Software Design I Dr. Gerardo Rosiles.
Creating PHP Pages Chapter 7 PHP Decisions Making.
Objectives Using functions to organize PHP code
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 2 Sanjay Goel University at Albany,
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
Loops – While, Do, For Repetition Statements Introduction to Arrays
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Guide To UNIX Using Linux Third Edition
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Matlab Programming, part 1 M-files It is generally more convenient to program in Matlab using m-files, ascii text files containing a set of Matlab commands.
Essential Shell Programming by Prof. Shylaja S S Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of Technology, Bangalore
Linux+ Guide to Linux Certification, Third Edition
Introduction to Awk Awk is a convenient and expressive programming language that can be applied to a wide variety of computing and data manipulation tasks.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Awk Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
Sed, awk, & perl CS 2204 Class meeting 13 *Notes by Mir Farooq Ali and other members of the CS faculty at Virginia Tech. Copyright 2003.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Revision Lecture Mauro Jaskelioff. AWK Program Structure AWK programs consists of patterns and procedures Pattern_1 { Procedure_1} Pattern_2 { Procedure_2}
JavaScript, Fourth Edition
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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
The awk command. Introduction Awk is a programming language used for manipulating data and generating reports. The data may come from standard input,
JavaScript, Sixth Edition
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
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
Control Structure  What is control Structure?  Types of Controls  Use the control structure in VBScript.  Example Summery.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this.
PROGRAMMING: What’s It All About?
awk- An advanced Filter
CSC 4630 Meeting 7 February 7, 2007.
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Arrays: Checkboxes and Textareas
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
CHAPTER 4 CLIENT SIDE SCRIPTING PART 2 OF 3
Topics discussed in this section:
ITM 352 Flow-Control: Loops
John Carelli, Instructor Kutztown University
Outline Altering flow of control Boolean expressions
Chapter 9 Structuring System Requirements: Logic Modeling
Control Structures: for & while Loops
Web DB Programming: PHP
awk- An Advanced Filter
Objectives In this chapter, you will:
Your questions from last session
Essential Shell Programming
Introduction to Computer Science
Chapter 9 Structuring System Requirements: Logic Modeling
Midterm Review October 23, 2006 ComS 207: Programming I (in Java)
Essential Shell Programming
Essential Shell Programming
CIS 136 Building Mobile Apps
LOOPING STRUCTURE Chapter - 7 Padasalai
Chapter 13 Control Structures
Essential Shell Programming
Presentation transcript:

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 shylaja.sharath@pes.edu

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 else da = 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/ISEDEPT PATH=/usr/bin::/usr/local/bin::/usr/bin TERM=xterm HOME=/home/ISEDEPT SHELL=/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 4 chairman 1 executive 2 director 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=0 while (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