Programming Languages Meeting 13 December 2/3, 2014.

Post on 01-Jan-2016

216 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

Transcript

Programming Languages

Meeting 13December 2/3, 2014

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

Matrix System

• Remember that recursion is your friend.• Questions?

Continuing with AWK

• AWK statement formatpattern

{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)

Your Turn (1)

Write an AWK program to:Determine how many flights were cancelled

/Cancelled/ {count++}

END {print count, “flights were cancelled”}

Running the programawk –f cancelled.awk flightaware.txt

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 programawk –f latest.awk flightaware.txt

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?

Your Turn (4)

Write an AWK program toList 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.

Constructing Patterns

• Arithmetic comparisonsNF > 0

• Regular expressions– Coming next

• Combinations with logical operators && || !

• Matching~ !~

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)

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

Regular Expressions (3)

• A . matches any (one) character.Examples:

A.b matches Aeb, A$b, A_b, Abb

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

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

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.

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

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.

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\.$/

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 dataa. 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?

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.

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-4505a. 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

top related