You are on page 1of 3

# Introduction

-------------Lab exercises (10%)


Two assignments (30%)
Exam (60%) - must pass exam + solve two of the programming questions
# Reading Material
-----------------The Practice of Programming - Kernighan & Pike
Code Complete (2ed) - McConnell
Learning Perl (5ed) = Schwartz, Phoenix & Foy
Unix Shell Programming - Kochgan & Wood 2003
# Filters
--------"filter": program that transforms a data stream
# argc
-----If argc == 1, then there are no command line arguments
# Interrupt Program
------------------Ctrl + C will stop program
# Standard Input
---------------This is a stream of bytes coming into your program.
# significance of ? in command line
----------------------------------i.e. wc hp?.txt
This will count for files hp1.txt, hp2.txt, hp5.txt, etc.
# Using Filters
--------------If no files are given in command line, then the filter reads from standard input
.
To read from file data1:
filter data1
OR
filter < data1
To read from multiple files:
filter data1 data2 data3
To read from data1, standard input (stdin), then data2:
filter data1 - data2
Filter options introduced by a "-":
They have a "short" form (-v) and a "long" form (-verbose)

Short forms can usually be combined (-av vs -a -v)


--help or -? often give list of all command-line options
To find out what filters are available: apropos keyword
### Lecture Two
===============
## ls | wc
---------Using "word counter" filter on directory
Will show number of files in directory, number of lines,
and number of characters in file names
## cat | cat | etc...
--------------------Put "hello" into stdin and "hello" will be printed out.
## head/tail
-----------Head copies first 10 lines of input, and tail last 10 lines.
ls|head -3
will provide you with first three files in directory
tail -2 wc.c
will provide you with last two lines in wc.c file
head -1000 file.c|tail -1
will provide you with the 1000th line in the file.c
## egrep
-------Grep is case-sensitive. To ignore case, go "egrep -i"
"-v" will display lines that do not match the pattern
egrep Wool text.c
Will provide you with all lines with a mention of "Wool" in it
egrep -i Sheep hp?.txt|egrep Harry
Will find lines with sheep and harry in it (non case-sensitive).
egrep Andrew
Will repeat back any stdin lines with "Andrew" in them
egrep -i computing course_codes | egrep -v COMP | wc
Will provide all courses with computing (non case sensitive)
as well as anything without COMP and number of lines (first number)
egrep '\|' test
to find lines containing "|" using egrep in file "test"
egrep 'Ron and Hermione' hp?.txt | wc.c
Will provide number of times this phrase is in a line in hp texts

## tr: transliterate characters

------------------------------tr 'sourceChars' 'destChars' < someText


Will convert text char by char (not permanently)
tr 'abc' '123' < someText where someText is a-z:
123defghijkl...etc.
will convert all a's to 1 and all b's to 2
shorthands available: 'a-z', 'A-Z', '0-9', 'a-e'
if no corresponding character (destChars is smaller than sourceChars)
last char in destChars is used
-c
-s
-d

map all characters not occurring in sourceChars (complement)


squeeze adjacent repeated characters out (only copy the first)
delete all characters in sourceChars (no destChars)

## Regular Expressions
---------------------A 'regular expression' (regexp) defines a set of strings.
Characters which have special meanings (* [ ^) can be avoided by
escaping them with \
## Regular Expressions Basics
----------------------------p* denotes zero or more repetitions of p
egrep 'ab*c'
a, followed by any number of b`s, followed by c
Back will work, abbc, will work, etc.
A dot matches any single character
egrep 'a.c'
a, followed by any letter, followed by c
e.g. abc, adc, aec
egrep 'Hermione.*Ron' hp?.txt
Hermione followed by any number of characters, followed by Ron.

You might also like