cs 403: programming languages
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
CS 403: Programming Languages
Lecture 21
Fall 2003
Department of Computer Science
University of Alabama
Joel Jones
Lecture 21 2
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
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=$PATHPATH=/bin:/usr/bin
case $# in0) echo ‘Usage: which command’ 1>&2; exit 2esacfor 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 fidoneexit 1 # not found
Lecture 21 4
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
$ cat makeHTMLcat << EOF<t>Hello $1 $2</t>EOF$ makeHTML Joel Jones<t>Hello Joel Jones</t>
Another way of doing substitution
In contrast to <<‘s’ which does no substitution
$ cat makeHTML2cat << ‘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 packageecho '# to unbundle, sh this file'for ido 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 fileecho makeHTML 1>&2cat >makeHTML <<'End of makeHTML'cat << EOF<t>Hello $1 $2</t>EOFEnd of makeHTMLecho makeHTML2 1>&2cat >makeHTML2 <<'End of makeHTML2'cat << 'EOF'<t>Hello $1 $2</t>EOFEnd of makeHTML2
Lecture 21 6
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Sub-shells, background mode, and job control$ sleep 5$$ (sleep 5; date) & date[1] 2682Thu 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 21 7
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Some examples of quoting
$ dateThu 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 21 8
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Filters
$ ls -ltotal 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 makeHTML2drwxr-xr-x 2 jones staff 68 Nov 13 00:41 subdir$ ls -l | grep ‘^d’
Pair Up: What is the output?
Lecture 21 9
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Regular Expressions and Filters
$ tail -3 /usr/share/dict/web2zythumZyzomysZyzzogeton$
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
c Any 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 21 10
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
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 21 11
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
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 linePair Up: Write a pipeline that prints the 10 most frequent words in its input.
Lecture 21 12
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Printing 10 most common words
cat $* | # tr doesn’t take filename argumentstr -sc A-Za-z ‘\012’ | # all non alpha become newlinesort |uniq -c | # get the countsort -n | # sort by counttail # prints 10 by default
Use the man command to look at for help man sort