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