------------------------- Week 13 Notes for CST8129 ------------------------- -Ian! D. Allen - idallen@idallen.ca *** Keep up on your readings (Course Outline: average 5 hours/week homework) Remember - knowing how to find out an answer is more important than memorizing the answer. Learn to fish! RTFM! (Read The Fine Manual) Announcement: Final Exam Dec 12 3 hours 11h30 - 14h30 ------- Review: ------- Exercise #11 - Lunar Lander Game Try the demo: ~alleni/bin/lunarlander Read the demo files under Notes: parabola.sh.txt g*.sh.txt Chapter 8 - Bourne Shell Programming 8.8 - trapping signals See Notes file: interrupts.sh.txt, input_trap.sh.txt Chapter 3 - Regular Expressions and Pattern Matching See 4.7 p.100 (with modifications): Ordinary regexp metacharacters: ^ $ . * [] Extended regexp metacharacters: ? + | () {} A backslash reverses the meaning of a regexp character. (If it was on, it turns it off; if it was off, it usually turns it on.) Chapter 4 - The grep Family: grep, egrep, fgrep egrep uses Extended regexp by default. fgrep doesn't use regexp at all. Chapter 5 - sed, the Streamlined(sic) Editor Basic regexp substitutions: sed -e 's/regexp/text/g' -e 's/regexp/text/g' [ files... ] ---------- This week: ---------- More on sed: Using & in LHS of s/// (see Example 5.16 p. 138) More extended regexp metacharacters (see "New with egrep" table p. 96): + - (extended) one or more -> x+ -> xx* ? - (extended) zero or one -> xy?z -> x(y|)z -> (xz|xyz) Chapter 4 - The grep Family: grep, egrep, fgrep grep family options: -v, -A, -B and -n, -l, -i, -c, -w Exercises: ALL Text errata: Q10 p.124 s/insensitive/sensitive/ Chapter 5 - sed, the Streamlined(sic) Editor sed options to know: -e, -n, -f addressing: /regexp/ 1 $ combo: /regexp/,23 23,/regexp/ 23,$ negation address range suffix: ! sed commands: a c d i p q s WARNING: y command doesn't take ranges like tr does! s/// substitution flags: g p Exercises: ALL Chapter 6 - The awk Utility On Linux awk and gawk are the same program, essentially equivalent to "nawk". Note: awk uses floating-point arithmetic, not integer! - be careful of testing equality with floating point! - echo hi | awk ' ((1/3)+1-1)*3 == 1 { print } ' <== NO OUTPUT! Read: 6.1.1 (p. 157) through 6.12.8 (p.202) - OMIT 6.4.2 (0FMT) - OMIT 6.4.3 (printf) - 6.6.3,6.10.4 not all versions of awk support a multiple character FS - avoid using multi-char FS or -F if you can - OMIT 6.11.2,6.12.7 ( ? : query conditional operator) built-in variables: NF NR field selection expressions: $1 $2 $NF $(NF-1) $(1+3) awk options: -F $ awk -Fx ' Boolean_Expression { action } ' [... files ...] simple Boolean Expressions: $1 == "hi" $1 < 5 /foobar/ $1 ~ /^[fF]oobar/ compound expressions can use arithmetic, ||, and && like C Language: ( $1+$2 ) >= 500 && $3 ~ /^Moneybags/ { print } WARNING: awk silently treats non-numeric strings as zero in arithmetic! $ echo one two three | awk '{ print $1+$2+$3 }' 0 <=== no error message! ---------- Next week: ---------- Read: 6.13.1 (p. 203) through 6.26.7 (p.278) - OMIT 6.14 redirection and pipes - OMIT 6.15 pipes - OMIT 6.16.6 printf - OMIT 6.16.7 redirection and pipes - OMIT 6.16.8 pipes - OMIT 6.23 user-defined functions - OMIT 6.23,6.26.6 user-defined functions - OMIT 6.25.1 fixed fields (but read use of gsub) - OMIT 6.25.2 multi-line records - OMIT 6.25.3 form letters - OMIT 6.26.2 time functions - OMIT 6.26.4 getline