CSC 4630 Meeting 2 January 22, 2007. Filters Definition: A filter is a program that takes a text...

9
CSC 4630 Meeting 2 January 22, 2007

Transcript of CSC 4630 Meeting 2 January 22, 2007. Filters Definition: A filter is a program that takes a text...

Page 1: CSC 4630 Meeting 2 January 22, 2007. Filters Definition: A filter is a program that takes a text file as an input and produces a text file as an output.

CSC 4630

Meeting 2

January 22, 2007

Page 2: CSC 4630 Meeting 2 January 22, 2007. Filters Definition: A filter is a program that takes a text file as an input and produces a text file as an output.

Filters

• Definition: A filter is a program that takes a text file as an input and produces a text file as an output.

• UNIX context– Write filters to use stdin as the input file and

stdout as the output file.– Use pipe to connect filters. Notation is the

vertical bar |

Page 3: CSC 4630 Meeting 2 January 22, 2007. Filters Definition: A filter is a program that takes a text file as an input and produces a text file as an output.

Filter History

• Originally conceived by M. D. McIlroy in the early 1970’s

• The UNIX notation for pipeline, the vertical bar, was introduced by K. L. Thompson.– “The Unix time-sharing system,” Comm. ACM,

July 1974.– “The Unix programming environment,”

Software Practice and Experience, January 1979.

Page 4: CSC 4630 Meeting 2 January 22, 2007. Filters Definition: A filter is a program that takes a text file as an input and produces a text file as an output.

Filter Examples

• detab -- replaces tab characters in a text file with the appropriate number of space characters

• entab -- replaces long strings of space characters with the appropriate number of tab characters

• compress – replaces long strings of the same character with a coding for the string and its length

Page 5: CSC 4630 Meeting 2 January 22, 2007. Filters Definition: A filter is a program that takes a text file as an input and produces a text file as an output.

Filter Examples (2)

• expand -- reverses the action of the compress filter

• translit – in simplest form, takes two argument strings of equal length and changes all occurrences of elements of first string into corresponding elements of second string.– Example: translit abc xyz changes all a’s to

x’s , b’s to y’s and c’s to z’s.

Page 6: CSC 4630 Meeting 2 January 22, 2007. Filters Definition: A filter is a program that takes a text file as an input and produces a text file as an output.

Simple Filters: head and tail

• Use head to look at the first few lines of a file– Keeps the first n lines of input and discards the

rest head [-n] [file]

• Use tail to look at the last few lines of a file– Keeps the last n lines of input and discards the

rest tail [-n] [file]

– Discards the first n-1 lines of input and keeps the rest

tail [+n] [file]

Page 7: CSC 4630 Meeting 2 January 22, 2007. Filters Definition: A filter is a program that takes a text file as an input and produces a text file as an output.

tr Command

•tr translates characters– Two arguments, given as strings– Three options, c d s– Often used for letter case conversion– Useful for “cleaning up” formatted output

Page 8: CSC 4630 Meeting 2 January 22, 2007. Filters Definition: A filter is a program that takes a text file as an input and produces a text file as an output.

tr Examples

• tr A-Z a-z Converts all upper case letters to lower case

• tr –s <tab><space> <space><space>– tr –s ‘\033\010’ ‘\010\010’

• tr –d <CR>^Z– tr –d ‘\015\032’

• tr –cs A-Za-z ‘\012’

Page 9: CSC 4630 Meeting 2 January 22, 2007. Filters Definition: A filter is a program that takes a text file as an input and produces a text file as an output.

tr Examples (2)

• tr a-b-d abcd

• tr a-c xyz

• tr -ac xyz

• tr \\

• tr –c \ x

• tr \c x