Lecture 14 Programming with awk II

Slides:



Advertisements
Similar presentations
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
Advertisements

Shell Scripting Awk (part1) Awk Programming Language standard unix language that is geared for text processing and creating formatted reports but it.
CIS 218 Advanced UNIX1 CIS 218 – Advanced UNIX (g)awk.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
C Tutorial Session #2 Type conversions More on looping Common errors Control statements Pointers and Arrays C Pre-processor Makefile Debugging.
C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat.
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
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.
13 More Advanced Awk Mauro Jaskelioff (originally by Gail Hopkins)
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Revision Lecture Mauro Jaskelioff. AWK Program Structure AWK programs consists of patterns and procedures Pattern_1 { Procedure_1} Pattern_2 { Procedure_2}
1 P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Awk Programming (2) Ruibin Bai (Room AB326) Division of Computer Science The University.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Computer Programming Control Structure
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Awk Programming (3) Ruibin Bai (Room AB326) Division of Computer Science The University.
CSCI 330 UNIX and Network Programming Unit IX: awk II.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
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
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
7 - Programming 7J, K, L, M, N, O – Handling Data.
PHP using MySQL Database for Web Development (part II)
awk- An advanced Filter
CSC 4630 Meeting 7 February 7, 2007.
Arrays Chapter 7.
Chapter 4 – C Program Control
Agenda Bash Shell Scripting – Part II Logic statements Loop statements
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
© 2016 Pearson Education, Ltd. All rights reserved.
Lecture 7: Repeating a Known Number of Times
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
John Carelli, Instructor Kutztown University
Looping.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, For loop While loop Do while loop
CNG 140 C Programming (Lecture set 8)
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
More Loops.
Chapter 2 - Introduction to C Programming
T. Jumana Abu Shmais – AOU - Riyadh
Iteration: Beyond the Basic PERFORM
String and Lists Dr. José M. Reyes Álamo.
Assignment Operators Topics Increment and Decrement Operators
Chapter 2 - Introduction to C Programming
Assignment Operators Topics Increment and Decrement Operators
Arrays Chapter 7.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays.
Arrays Topics Definition of a Data Structure Definition of an Array
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Functions continued.
More Loops Topics Counter-Controlled (Definite) Repetition
Chapter 2 - Introduction to C Programming
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Relational Operators Logical Operators for Loops.
More Loops Topics Counter-Controlled (Definite) Repetition
awk- An Advanced Filter
More Loops Topics Counter-Controlled (Definite) Repetition
Lec 6 Loop Statements Introduction to Computer Programming
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Assignment Operators Topics Increment and Decrement Operators
Arrays Topics Definition of a Data Structure Definition of an Array
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

Lecture 14 Programming with awk II

for loop This is a counted loop Will execute until the counter reaches the target value Can increment (count up) or decrement (count down) for also works with the elements of an array

Example BEGIN{ FS=":" } { if ($1 ~ /^root$|^uucp$/) line = "" for (i = NF ; i> 0 ; i--) line = line ":" $i print line }

while loop The while loop is an example of conditional execution The loop cycles as long as the condition specified is true A while loop always checks to see if it should execute Multiple actions must be enclosed in { }

Arrays awk handles one-dimensional array No array declarations are necessary The index of an array can be anything, even a string Array elements are automatically initialized

Example Consider the following file f1 (name and age of a person) Bart 10 Homer 38 Lisa 8 $awk '{age[$1]=$2} END {for(i in age) print age[i]}' f1 will print the age of each person

Example BEGIN { FS = ":" } { fullname = "" ; x = 0 ; array_length = split($5, name_arr," ") ; while ( x++ <= array_length ) { if (x < array_length) name_arr[x] = name_arr[x] "_" ; fullname = fullname name_arr[x] ; } printf "%s@heavens.com\n", fullname

Built-in Functions Built-in functions perform arithmetic and string operations. Arithmetic functions: int(x) returns integer value of x sqrt(x) returns square root of x

Built-in Functions String functions length() returns length of complete line length(x) returns length of x awk ‘length($2) < 10’ f1 substr(s, m) returns remaining string from position m in string s substr(s, m, n) returns portion of string of length n, starting from position m in string s awk ‘{print substr($1, 6, 2)}’ file

Built-in Functions String functions split(s, arr, ch) splits string s into array arr using ch as delimter; return number of fields system(“cmd”) runs UNIX command cmd and returns its exit status e.g. BEGIN { system(“tput clear”) system(“date”) }

User-Defined Functions Complicated awk programs can often be simplified by defining your own functions. Definitions of functions can appear anywhere in an awk program. awk reads the entire program before starting to execute any of it. There is no need to put the definition of a function before all uses of the function. Syntax: function name(parameter-list) { body-of-function }

User-Defined Functions Example: function myprint(num) { printf "%d\n", num } $3 > 0 { myprint($3) }

A Sample Data File Bird, Tweety 81 82 79 89 Cat, Sylvester 93 97 89 91 NAME TEST1 TEST2 TEST3 TEST4 Bird, Tweety 81 82 79 89 Cat, Sylvester 93 97 89 91 Coyote, Wiley 78 86 81 79 Duck, Daffy 71 78 83 81 Duck, Donald 87 83 91 89  

Suppose we want to display only the first names of the students whose test marks are saved in this file. - first try: awk ‘{ print NR, $2 }’ class.data   - output: 1 TEST1 2 Tweety 3 Sylvest 4 Wiley 5 Daffy 6 Donald 7

Specifically we instruct awk to process only those lines which begin with an upper case letter awk ‘/^[A-Z]/ { print NR, $2 }’ class.data 2 Tweety 3 Sylvester 4 Wiley 5 Daffy 6 Donald

Display the current line number after subtracting a 1 Display the current line number after subtracting a 1. awk ‘/^[A-Z]/ { print NR-1, $2 }’ class.data 1 Tweety 2 Sylvester 3 Wiley 4 Daffy 5 Donald

To demonstrate awk as a full programming language we tackle the following problem: Compute the average mark earned by each student in the file. Display the student’s first name followed by the student’s average.

if ( NR > 1 ) # skip the titles line # averages.awk { total = 0 count = 0 i = 3 if ( NR > 1 ) # skip the titles line while ( i <= NF ) # add marks in this line total += $i   count++ i++ } # while if ( count > 0 ) # prevent divide by zero avrg = total/count print ( $2, avrg ) # display the answer }

here is the awk command line: awk –f averages.awk class.data The Output   Tweety 82.75 Sylvester 92.5 Wiley 81 Daffy 78.25 Donald 87.5

A Useful Link http://www.math.utah.edu/docs/info/gawk_toc.html