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.

Slides:



Advertisements
Similar presentations
CST8177 awk. The awk program is not named after the sea-bird (that's auk), nor is it a cry from a parrot (awwwk!). It's the initials of the authors, Aho,
Advertisements

Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
1 Unix Talk #2 AWK overview Patterns and actions Records and fields Print vs. printf.
2000 Copyrights, Danielle S. Lahmani UNIX Tools G , Fall 2000 Danielle S. Lahmani Lecture 6.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming
Basic Elements of C++ Chapter 2.
Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
Introduction to Advanced UNIX March Kevin Keay.
Agenda Sed Utility - Advanced –Using Script-files / Example Awk Utility - Advanced –Using Script-files –Math calculations / Operators / Functions –Floating.
Introduction to Unix (CA263) File Processing. Guide to UNIX Using Linux, Third Edition 2 Objectives Explain UNIX and Linux file processing Use basic file.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Numeric Processing Chapter 6, Exploring the Digital Domain.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Input, Output, and Processing
CS 403: Programming Languages Fall 2004 Department of Computer Science University of Alabama Joel Jones.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Essential Shell Programming by Prof. Shylaja S S Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of Technology, Bangalore
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.
Programmable Text Processing with awk Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users”
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Awk Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
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.
Introduction to Unix – CS 21 Lecture 12. Lecture Overview A few more bash programming tricks The here document Trapping signals in bash cut and tr sed.
Chapter 12: gawk Yes it sounds funny. In this chapter … Intro Patterns Actions Control Structures Putting it all together.
Revision Lecture Mauro Jaskelioff. AWK Program Structure AWK programs consists of patterns and procedures Pattern_1 { Procedure_1} Pattern_2 { Procedure_2}
BY A Mikati & M Shaito Awk Utility n Introduction n Some basics n Some samples n Patterns & Actions Regular Expressions n Boolean n start /end n.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Chapter Twelve sed, awk & perl1 System Programming sed, awk & perl.
Chapter Six Introduction to Shell Script Programming.
CSCI 330 UNIX and Network Programming Unit IX: Shell Scripts.
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
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Awk Programming Ruibin Bai (Room AB326) Division of Computer Science The University.
CISC 1480/KRF Copyright © 1999 by Kenneth R. Frazer 1 AWK q A programming language for handling common data manipulation tasks with only a few lines of.
The awk command. Introduction Awk is a programming language used for manipulating data and generating reports. The data may come from standard input,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Sed. Class Issues vSphere Issues – root only until lab 3.
1 Lecture 10 Introduction to AWK COP 3344 Introduction to UNIX.
FILTERS USING REGULAR EXPRESSIONS – grep and sed.
By Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Awk 2 – more awk. AWK INVOCATION AND OPERATION the "-F" option allows changing Awk's "field separator" character. Awk regards each line of input data.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
SIMPLE FILTERS. CONTENTS Filters – definition To format text – pr Pick lines from the beginning – head Pick lines from the end – tail Extract characters.
Arun Vishwanathan Nevis Networks Pvt. Ltd.
awk- An advanced Filter
INC 161 , CPE 100 Computer Programming
Chapter 2 - Introduction to C Programming
PROGRAMMING THE BASH SHELL PART IV by İlker Korkmaz and Kaya Oğuz
CS 403: Programming Languages
Chapter 2 - Introduction to C Programming
What is Bash Shell Scripting?
John Carelli, Instructor Kutztown University
Introduction to Advanced UNIX
Essential Shell Programming
Essential Shell Programming
Essential Shell Programming
awk- An Advanced Filter
Introduction to Bash Programming, part 3
Introduction to C Programming
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

Session Objectives awk comparison operators Variables in awk. Use of –f Option BEGIN & END Sections Built-in Variables

The Comparison Operators awk also provides the comparison operators like >, =, <=,==, !=, etc.., Example 1 : $ awk –F “|” ‘$3 == “manager” || $3 == “chairman” { printf “%-20s %-12s %d\n”, $2, $3, $5}’ emp.lst

Contd.. Output: The above command looks for two strings only in the third field ($3). The second attempted only if (||) the first match fails. Note: awk uses the || and && logical operators as in C and UNIX shell.

Contd.. Example 2 : $ awk –F “|” ‘$3 != “manager” && $3 != “chairman” { printf “%-20s %-12s %d\n”, $2, $3, $5}’ emp.lst Output:

Contd.. The above example illustrates the use of != and && operators. Here all the employee records other than that of manager and chairman are displayed.

Contd.. ~ and !~ : The Regular Expression Operators: In awk, special characters, called regular expression operators or metacharacters, can be used with regular expression. It increase the power and versatility of regular expressions.

Contd.. Example1: $2 ~ /[cC]ho[wu]dh?ury / || $2 ~ /sa[xk]s?ena / Matches second field Example2: $2 !~ /manager | chairman / Neither manager nor chairman Note: The operators ~ and !~ work only with field specifiers like $1, $2, etc.,.

Contd.. For instance, to locate g.m s the following command does not display the expected output, because the word g.m. is embedded in d.g.m or c.g.m. $ awk –F “|” ‘$3 ~ /g.m./ {printf “….. prints fields including g.m like g.m, d.g.m and c.g.m

Contd.. To avoid such unexpected output, awk provides two operators ^ and $ that indicates the beginning and end of the field respectively. So the above command should be modified as follows: $ awk –F “|” ‘$3 ~ /^g.m./ {printf “….. prints fields including g.m only and not d.g.m or c.g.m

Contd..

Number Comparison: awk has the ability to handle numbers (integer and floating type). Relational test or comparisons can also be performed on them. Example: $ awk –F “|” ‘$5 > 7500 { printf “%-20s %-12s %d\n”, $2, $3, $5}’ emp.lst

Contd.. Output: In the above example, the details of employees getting salary greater than 7500 are displayed.

Contd.. Regular expressions can also be combined with numeric comparison. Example: $ awk –F “|” ‘$5 > 7500 || $6 ~/80$/’ { printf “%- 20s %-12s %d\n”, $2, $3, $5, $6}’ emp.lst

Contd.. Output: Here, details of employees getting salary greater than 7500 or whose year of birth is 1980 are displayed.

Contd.. Number Processing: Numeric computations can be performed in awk using the arithmetic operators like +, -, /, *, % (modulus). One of the main feature of awk w.r.t. number processing is that it can handle even decimal numbers, which is not possible in shell.

Contd.. Example: $ awk –F “|” ‘$3’ == “manager” { printf “%-20s %-12s %d\n”, $2, $3, $5, $5*0.4}’ emp.lst Output: In the above example, DA is calculated as 40% of basic pay.

Variables awk allows the user to use variables of there choice. Example 1 : $ awk –F”|” ‘$3 == “director” && $6 > 6700 { >kount =kount+1 >printf “ %3f %20s %-12s %d\n”, >kount,$2,$3,$6 }’ empn.lst

Contd.. This prints a serial number, using the variable kount, and apply it those directors drawing a salary exceeding 6700: Output: 1 lalit chowdury manager barun sengupta manager jai sharmamanager 7000

Contd.. The initial value of kount was 0 (by default), hence first line is assigned the number 1. awk also accepts the C- style incrementing forms: Kount ++ Kount +=2 Printf “%3d\n”, ++kount

The –f Option: Storing awk Programs In A File Normally awk programs are stored in separate file with.awk extension for easier identification Example : $ cat emp.awk $3 == “director” && $6 > 6700 { printf “%3d %- 20s %-12s %d\n”, ++kount, $2, $3, $6 }

Contd.. Here quotes are not used to enclose the awk program. Instead –f filename option can be used to obtain the same output: awk –F “|” –f emp.awk emp.lst Note: When –f option is used, program stored in the file should not be enclosed within quotes.

THE BEGIN AND END SECTIONS awk statements are applied to all lines selected by the address. If there are no addresses, then they are applied to every line of input. To print something before processing the first line, for example, a heading, then the BEGIN section can be used.

Contd.. Similarly, the end section useful in printing some totals after processing is over. The BEGIN and END sections are optional Syntax: BEGIN {action} END {action} Note: Both of them use curly braces. These two sections, when present, are delimited by the body of the awk program.

Contd.. Example: BEGIN { printf “\t \t Employee Details\n\n” } $6 > 8000 { count ++; total+=$6 printf “%3d %-20s %-12s %d\n”, count, $2, $3, $6 } END { printf “\n\t Avg pay is %6d\n”, total/count }

Contd.. Here BEGIN section prints a suitable heading offset by two tabs (\t \t). The END section prints the average pay (tot / count) for the selected lines. To execute this program, use the –f option: $ awk –F “|” –f emp.awk emp.lst

Contd.. Output: Employee Details 1 ramesh g.m rakesh g.m chowdury manager jai sharma manager 9000 Avg. pay is 8550.

Contd.. Like other filters, awk reads standard input when the filename is omitted. awk can be made to work like a simple scripting language by using BEGIN section. For instance,floating point arithmetic can be performed as illustrated below: $ awk ‘BEGIN {printf “%f\n”, 22/7 }’

Built-in variables Awk has several built-in variables. They are all assigned automatically, though it is also possible for a user to reassign some of them. The FS Variable: awk ues a contiguous string of spaces as the default field delimeter.

Built-in variables FS redefines this field separator, When used,it must occur in the BEGIN section so that the body of the program knows its value before it starts processing: BEGIN {FS=”|”} This is an alternative to the –F option which does the same thing.

Built-in variables The OFS Variable: print statement used with comma-separated arguments, each argument was separated from the other by a space in the output. This is awk’s default output field separator Can reassigned using the variable OFS in the BEGIN section: BEGIN { OFS=”~” }

Built-in variables EX: By using it on a file, say emp.lst, we can locate those lines not having 6 fields, and which have crept in due to faulty data entry: $awk ‘BEGIN { FS = “|” } NF !=6 { Print “Record No “, NR, “has ”, “fields”}’ emp.lst

Built-in variables The FILENAME Variable: FILENAME stores the name of the current file being processed. EX:‘$6<4000 {print FILENAME, $0 }’ With FILENAME, we can device logic that does different things depending on the file that is processed.

Conclusion Through this session, we came to know about usage of comparisons operators and regular expression operators. We also saw how number processing can be done in awk. Additionally we learnt the use of –f option, BEGIN and END Sections in awk program which enable us to write reports. Finally the use of built in variables used for different purposes.