Presentation is loading. Please wait.

Presentation is loading. Please wait.

John Carelli, Instructor Kutztown University

Similar presentations


Presentation on theme: "John Carelli, Instructor Kutztown University"— Presentation transcript:

1 John Carelli, Instructor Kutztown University carelli@kutztown.edu

2 Awk (Programming/Scripting Language)
Features: programmable shares many constructs with “C” … but has a number of extensions (ex: Associative Arrays) interpreted data-driven designed for ease of use Bell Labs (Aho, Weinberger, Kernighan) Versions: awk gawk nawk

3 awk [options] [program] [file-list]
Syntax: awk [options] [program] [file-list] awk [options] –f program-file [file-list] Input text Records Fields awk program pattern { action }

4 Awk – default operation
Input processed one line at a time Each line is a record built-in variable $0 Each record is automatically parsed Broken into tokens Each token is a field built-in variables $1 to $n where n is the number of fields default field delimiter is whitespace (\s)

5 Awk – basic program An awk program consist of pattern/action lines
syntax: pattern { action } patterns Match using RegEx based commands on the current line actions If matched, perform some operation on the line

6 BEGIN END Expression  ~  !~

7 if (conditional) then action [ else action]
while (conditional) action for (counter; conditional; increment) action for (variable in array) action do action while (conditional) break continue variable = expression print [list of expressions] [> expression] printf format [,list of expressions] [> expression] -> a comma will print a space next exit {list of statements}

8 FILENAME – name of the input file
NF – number of fields in the current line NR – line number of the input stream $0 – the entire line $1 – first field of the current line $2 – second field of the current line $3, $4, etc. – rest of fields FNR – record number in current file FS – input field separator OFS - output field separator

9 Awk commands (no single quotes)
awk –f # Comments

10 Statements Expression String literals Assignment operator: =
Logical operators: || && ! Matching operators: ~ !~ Relational operators: < <= Arithmetic operators: + - * = = != > >= / % ^ Increment and decrement operators: Parenthesis String literals

11 int(num) Returns integer portion of num exp(num) log(num) sqrt(num) length(str) Length of the string substr(str,pos,l en) Returns substring of str that begins at pos for len characters index(str1, str2) Returns index of str2 in str1 or 0 if str2 is not present in str1 split(str, arr, del) Places elements of str, delimited by del, in the array arr; Returns the number of elements in the array arr printf and sprint Similar to C tolower / toupper

12 printf formatString, var1, var2, ….
C-style output formatting - (before C++ stream objects) printf formatString, var1, var2, …. formatString formats listed variables in the order given conversion specifiers get embedded in the formatString conversion specifiers begin with %

13 printf – conversion specifiers
Basic Syntax: % flags width [ . precision ] type if flag is a minus sign, left justify width is the character width of the output padded, if necessary precision (for floating point values) basic types: d for integers s for strings f (fixed) or e (scientific) for floats

14 Defintion array[string-key] = value; for (<elem> in <array>) <action>;

15 if / if-else while for break continue

16 Print each registrant, the conference and the amount due from conf
Print each registrant, the conference and the amount due from conf.data Print company, name and conference for all registrants that don’t owe money Print name and amount due for registrants attending the Coatings conference and that owe money Previous example but print headings Print the number of unique conferences being held

17 Write a program that calculates the average amount paid by all participants in the conf.data file. In addition, calculate the average amount owed by each participant, and print these values. Write a program that calculates the amount that was paid by each registrant. For each registrant, print out the name of the registrant, the amount paid, and the average amount paid by all registrants. Write a program that will print the number of registrants registered for the Adhesions conference, Coatings conference and Resins conference

18 Write a program using the conf.data file that does the following:
Calculate and print the number of people registered for conferences who come from Digital Calculate and print the total amount owed for people from Digital Calculate the average amount owed for registrants from Digital


Download ppt "John Carelli, Instructor Kutztown University"

Similar presentations


Ads by Google