Assignment 3: Regular Expressions

Here is updatede top2.tar with files from Tuesdays meeting and the problems below. Untar with tar xf top2.tar and remove with /bin/rm -rf top2. Here are some questions about the file `words' in top2/text/. Read the page 'man 5 regexp' (man -s 5 regexp on solaris).
  1. Use grep (/bin/grep on the math machines) word to pick out the lines in the word file which satisfy:
    1. Blank lines.
    2. lines with at least 3 characters
    3. Exactly 3 letters from abc
    4. Of the format non-a followed by 3 to 5 a's followed by a non-a
    5. lines without the punctuation
    6. lines with an upper case letter
    7. lines with abc or cba or bdc
  2. Use sed -e 's///' words to change all the lines in words file so that.
    1. Blank lines change to "this line is intentionally left blank" (sans ")
    2. Lines with 3 characters are replaced with XXX
    3. Replace all a or A's with 7
    4. Replace all aaa, aaaa, or aaaaa with @
    5. Replace uppercase letters with periods
    6. replace periods with P's
    7. replace aa...a strings with "" where the aa..a is the same length as the original
  3. Explain the following outputs
    unix> echo aaaaaaa | sed -e 's/a/X/'
    Xaaaaaa
    unix> echo aaaaaaa | sed -e 's/a*/X/'
    X
    unix> echo aaaaaaa | sed -e 's/b*/X/'
    Xaaaaaaa
    unix> echo aaaaaaa | sed -e 's/a/X/g'
    XXXXXXX
    unix> echo aaaaaaa | sed -e 's/aaa/X/g'
    XXa
    unix> echo aaaaaaa | sed -e 's/a\{3,5\}/X/g'
    Xaa
    
  4. Modify this grep-like perl function to be sed-like so that script aa bb foo will act something like sed -e 's/aa/bb/' foo. The print "$line"; could be replaces with "print "$`$1$'";" where $` is the part before the match, and $' is the part after the match. Or you could use s/aa/bb/ operator.
    #!/bin/perl
    # (perl might be somewhere else)
    if ( $#ARGV < 1 ) {
    	print "usage $0:  \n";
    	exit 1;
    }
    $regexp = $ARGV[0];
    $filename = $ARGV[1];
    open(FH, $filename);
    while() {
    	$line = $_;
    	if ( $line =~ /($regexp)/ ) {
    		# matches
    		print $line;
    	} else {
    		# matches not
    	}
    }
    close(FH);