Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Languages Meeting 13 December 2/3, 2014.

Similar presentations


Presentation on theme: "Programming Languages Meeting 13 December 2/3, 2014."— Presentation transcript:

1 Programming Languages Meeting 13 December 2/3, 2014

2 Planning Matrix System: Lisp code and semantic specifications due Thursday, December 4 by 11:59 pm. Next week: Finish scripting languages; review for final Final Exam: Tuesday, December 16, 2014, 2:30 – 5:00 pm, MSC 290

3 Matrix System Remember that recursion is your friend. Questions?

4 Continuing with AWK AWK statement format pattern {action} pattern {action} Defaults – No action: print current line, namely {print $0} – No pattern: every line matches, that is the action is performed for every line in the input file(s)

5 Your Turn (1) Write an AWK program to: Determine how many flights were cancelled /Cancelled/ {count++} END {print count, “flights were cancelled”} Running the program awk –f cancelled.awk flightaware.txt

6 Your Turn (2) Write an AWK program to: Find the latest arriving flight from those in the list {if ($11 > latest) latest = $11} END {print latest} Running the program awk –f latest.awk flightaware.txt

7 Your Turn (3) Write an AWK program to: Find the earliest arriving flight from those in the list Try modifying your “latest” program by switching the inequality. What else do you have to change to make the program work?

8 Your Turn (4) Write an AWK program to List the different aircraft used for this flight number. {type[$2]++} END {for (a in type) print a} Modify the program to print the frequency of use for each type. Modify again to remove the cancelled flights.

9 Constructing Patterns Arithmetic comparisons NF > 0 Regular expressions – Coming next Combinations with logical operators && || ! Matching ~ !~

10 Regular Expressions A single character not otherwise endowed with special meaning matches that character. Example: m matches any string containing an m – Two regular expressions concatenated match a match of the first pattern followed immediately by a match of the second. Examples: mc matches any string containing mc Robert matches any string containing Robert (this is a concatenation of 6 regular expressions)

11 Regular Expressions (2) Characters with special meaning in certain contexts are: \ [ ]. ^ $ * + ? ( ) | Two questions: 1.What are the “certain contexts” that give rise to “special meanings”? 2.What if you don’t want a “special meaning”? Answer: A \ followed by a single character matches that character. Examples: \$ matches the dollar sign $ \\ matches the backslash \\ \\\\ matches two backslashes in a row

12 Regular Expressions (3) A. matches any (one) character. Examples: A.b matches Aeb, A$b, A_b, Abb

13 Regular Expressions (4) A string enclosed in brackets [ ] matches any single character from the string. Some characters in the string may have special meanings depending on their positions. [aeiou] matches a line with a vowel in it ] as a string element must be the first character. []a] matches either ] in the line or a in the line

14 Regular Expressions (5) - between two characters in ascending ASCII order denotes the inclusive set of characters; otherwise, it has no special meaning. A-M denotes the first 13 upper case letters. ^ as the first string element indicates the complement of the string in the alphabet; otherwise, it has no special meaning. [^0-9] matches a string that has a character other than a digit in it [^a-zA-Z0-9] matches a string that has a least one symbol in it

15 Regular Expressions (6) 1.A regular expression followed by * matches a sequence of 0 or more matches of the regular expression. 2.A regular expression followed by + matches a sequence of 1 or more matches of the regular expression. 3.A regular expression preceded by ^ is constrained to matches at the beginning of the line. 4.A regular expression followed by $ is constrained to matches at the end of the line.

16 AWK Patterns One form of a pattern for AWK is a regular expression enclosed in a pair of slashes / / A pattern can be limited to one field by using the match (or does not match) symbols /plane/ ~ $2 /train/ !~ $4

17 AWK Patterns (2) Your turn: For each of the patterns on the next slide, describe the lines that would be matched by an AWK program using that pattern.

18 AWK Patterns (3) 1./F$/ 2./[xy]/ 3./Mc/ 4./ab*c/ 5./[A-Za-z]*/ 6./[0-9].[0-9]/ 7./$$/ 8./ab*c*/ 9./^[^A-Za-z0-9]/ 10./[JS]r\.$/

19 AWK Patterns (4) 1.For the web form data, generate a list of the properly constructed US and Canada telephone numbers. 2.Find all proper names in the Moby Dick extract. Caution: This is an html file, so tags abound 3.For the countries data a.What is the population of Asia? b.What percentage of the world’s population lives in Asia? c.How many countries the size of Sudan will fit inside Canada?

20 AWK Exercises 1.Given a file of words, one per line, write an AWK script that returns the frequency count of the letters in the words. Use a template that – has one action statement in body, a for loop – has one statement for the END pattern, a for loop that controls the printing – uses one user-defined variable, an array called lc – uses the substring function, substr, to split each word into its individual characters.

21 AWK Exercises 2.Suppose each line of a file looks like the 14 character string that represents a US phone number in cellphone standard form (610) 519-4505 a.How many fields are there in the string if you are using default FS? What are their lengths? b.What if FS = “ - ” ? 3.Neither of these answers is acceptable for processing phone numbers. Reformat the string (610) 519-4505 and others of this form by using an AWK script with a single action statement and writing the string as 610- 519-4505


Download ppt "Programming Languages Meeting 13 December 2/3, 2014."

Similar presentations


Ads by Google