CS 403: Programming Languages Lecture 21 Fall 2003 Department of Computer Science University of Alabama Joel Jones
Lecture 212 Overview Announcements Story Hour, Houser 108, 3PM Friday Colloquium, Houser 108, 11PM Monday, received his Ph.D. from UA. Prof. Nenad Jukic Loyola University Chicago. “Comprehensive Data Warehouse Exploration: Extending the Scope of Association-Rule Mining” MP2 Shell Programming Substitution Sub-shells, background mode, job control Quoting Regular Expressions and filters
Final version of which # which cmd: which cmd in PATH is executed, final version opath=$PATH PATH=/bin:/usr/bin case $# in 0) echo ‘Usage: which command’ 1>&2; exit 2 esac for i in `echo $opath | sed ‘s/^:/.:/ s/::/:.:/g s/:$/:./ s/:/ /g’` do if test -f $i/$1 then echo $i/$1 # found it exit 0 fi done exit 1 # not found
Lecture 214 $ cat makeHTML cat << EOF Hello $1 $2 EOF $ makeHTML Joel Jones Hello Joel Jones Another way of doing substitution In contrast to <<‘s’ which does no substitution $ cat makeHTML2 cat << ‘EOF’ Hello $1 $2 EOF $ makeHTML2 Joel Jones Hello $1 $2
Programs that write programs $ cat bundle # bundle: group files into distribution package echo '# to unbundle, sh this file' for i do echo "echo $i 1>&2" echo "cat >$i <<'End of $i'" cat $i echo "End of $i" done $./bundle makeHTML makeHTML2 > junk $ cat junk # to unbundle, sh this file echo makeHTML 1>&2 cat >makeHTML <<'End of makeHTML' cat << EOF Hello $1 $2 EOF End of makeHTML echo makeHTML2 1>&2 cat >makeHTML2 <<'End of makeHTML2' cat << 'EOF' Hello $1 $2 EOF End of makeHTML2
Lecture 216 Sub-shells, background mode, and job control $ sleep 5 $ $ (sleep 5; date) & date [1] 2682 Thu Nov 13 00:19:31 CST 2003 $ Thu Nov 13 00:19:36 CST 2003 $ (sleep 300; echo Tea is ready) & [1] 2684 $jobs [1] + Running (sleep 300; echo Tea is ready) & $ kill %1 [1] + Terminated (sleep 300; echo Tea is ready) & $
Lecture 217 Some examples of quoting $ date Thu Nov 13 00:28:08 CST 2003 $ echo "The time is `date`" Pair Up: What is the output? $ `date` Pair Up: What is the output? $ echo `echo \`date\`` Pair Up: What is the output?
Lecture 218 Filters $ ls -l total 32 -rwxr--r-- 1 jones staff 203 Nov 13 00:09 bundle -rw-r--r-- 1 jones staff 240 Nov 13 00:10 junk -rwxr--r-- 1 jones staff 34 Nov 13 00:00 makeHTML -rwxr--r-- 1 jones staff 36 Nov 13 00:04 makeHTML2 drwxr-xr-x 2 jones staff 68 Nov 13 00:41 subdir $ ls -l | grep ‘^d’ Pair Up: What is the output?
Lecture 219 Regular Expressions and Filters $ tail -3 /usr/share/dict/web2 zythum Zyzomys Zyzzogeton $ Pair Up: write a grep command to find all words that contain all fives vowels, in order, e.g. abstemious. Format: grep pattern file* Grep Regular Expressions cAny non-special character c matches itself ^Beginning of line $End of line […]Any one of the characters in …; ranges like a-z are legal [^…]Any single character not in …; ranges are legal r*Zero or more occurrences of r
Lecture 2110 Pair Up: Write a sort command that prints unique lines doing a case insensitive comparison. Other Filters Sort has lots of options -f folds upper and lower case -n sorts by numeric value -r reverses order +m skips first m fields, +0 stands for entire line -u (unique) removes adjacent duplicate lines Multiple field specifications do lexigraphic sorting—lines considered equal by earlier sorts are grouped and kept together as subsequent sorts sort each group sort +0f +0 -u filenames
Lecture 2111 Other Filters (cont.) tr inputChars outputChar(s) tr a-z A-Z maps lower case to upper case Flags: -s squeezes multiple occurences of a character in the input to a single character in the output; -c takes the complement of the first argument, e.g. tr -c ab matches every character except a and b. tr also understands character ranges. uniq removes duplicate adjacent lines Flags: -c adds count of duplicate lines at beginning ‘\012’ is a new line Pair Up: Write a pipeline that prints the 10 most frequent words in its input.
Lecture 2112 Printing 10 most common words cat $* | # tr doesn’t take filename arguments tr -sc A-Za-z ‘\012’ | # all non alpha become newline sort | uniq -c | # get the count sort -n | # sort by count tail # prints 10 by default Use the man command to look at for help man sort