/' words to
change all the lines in words file so that.
- Blank lines change to "this line is intentionally left blank" (sans ")
- Lines with 3 characters are replaced with XXX
- Replace all a or A's with 7
- Replace all aaa, aaaa, or aaaaa with @
- Replace uppercase letters with periods
- replace periods with P's
- replace aa...a strings with "" where the aa..a is the same
length as the original
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
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);