CS 403: Programming Languages Lecture 21 Fall 2003 Department of Computer Science University of Alabama Joel Jones
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
Another way of doing substitution $ cat makeHTML cat << EOF <t>Hello $1 $2</t> EOF $ makeHTML Joel Jones <t>Hello Joel Jones</t> • In contrast to <<‘s’ which does no substitution $ cat makeHTML2 cat << ‘EOF’ <t>Hello $1 $2</t> EOF $ makeHTML2 Joel Jones <t>Hello $1 $2</t>
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 <t>Hello $1 $2</t> EOF End of makeHTML echo makeHTML2 1>&2 cat >makeHTML2 <<'End of makeHTML2' cat << 'EOF' <t>Hello $1 $2</t> EOF End of makeHTML2
Sub-shells, background mode, and job control $ sleep 5 $ $ (sleep 5; date) & date  2682 Thu Nov 13 00:19:31 CST 2003 $ Thu Nov 13 00:19:36 CST 2003 $ (sleep 300; echo Tea is ready) &  2684 $jobs  + Running (sleep 300; echo Tea is ready) & $ kill %1  + Terminated (sleep 300; echo Tea is ready) & $
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?
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?
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*
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 Other Filters Pair Up: Write a sort command that prints unique lines doing a case insensitive comparison. sort +0f +0 -u filenames
Other Filters (cont.) • tr inputCharsoutputChar(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.
Printing 10 most common words • Use the man command to look at for help • man sort 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