cs 403: programming languages

12
QuickT TIFF (Un are need CS 403: Programming Languages Lecture 21 Fall 2003 Department of Computer Science University of Alabama Joel Jones

Upload: ownah

Post on 05-Jan-2016

33 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: CS 403: Programming Languages

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

Page 2: CS 403: Programming Languages

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

Page 3: CS 403: Programming Languages

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

Page 4: CS 403: Programming Languages

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>

Page 5: CS 403: Programming Languages

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

Page 6: CS 403: Programming Languages

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) &$

Page 7: CS 403: Programming Languages

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?

Page 8: CS 403: Programming Languages

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?

Page 9: CS 403: Programming Languages

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

Page 10: CS 403: Programming Languages

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

Page 11: CS 403: Programming Languages

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.

Page 12: CS 403: Programming Languages

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