CSCI 344
Patterns and Substitution
Lab 12
Write the following Perl scripts. You may use the Internet as a
reference for Perl and as a reference for UNIX/Linux
commands, but you may not search for
solutions to these problems--that would completely defeat the purpose
of doing them.
Here is a link to a good intro to Perl: perldoc.perl.org/perlintro.html
You may NOT use any UNIX/Linux command or utility to solve these problems. They can all be solved with basic Perl.
Note: For problems 1, 2, and 3
if there are command line arguments assume they are all filenames and read
from the corresponding files. If no filenames are given, read from standard input. Write modified
text to standard input. Hint: Perl makes this really, really easy.
1) (weight 1) Without using the split command, substitutes all commas
in the input to * in the output. Do not use any loops.
2) (weight 1) Using split, substitutes all commas in the input to * in the output. Do not use any loops.
3) (weight 2) Swap the first and last names in the input. Assume
first names cannot contain any spaces but last names can contain spaces. Assume
that the address always starts with a digit. Make sure your
output does not have unnecessary spaces before and after the names.
Input:
Joe Smith 123 Main St, Chico CA 95929
Sam De Jones 456 Elm Ave, Los Angeles CA 91101
Output:
Smith, Joe 123 Main St, Chico CA 95929
De Jones, Sam 456 Elm Ave, Los Angeles CA 91101
4) (Weight 2) Write a simple grep-like program that takes a pattern as
the first argument and filenames as the subsequent arguments (use STDIN
if no filenames given) . Print out each line in the input enclosing the first match using < > brackets.
$ mygrep "Tom" inputfile
She gave the ball to <Tom>'s brother.
$
If the first argument is "-i", assume the second argument is the pattern and make your search case insensitive.
5) (Weight 4) Write a program like the C-preprocessor that does C-style macro substitution:
If the following appears in the input:
#define foo FOO
every "foo" in the input should be replaced with "FOO" in the output.
For example, given the following input file (the ; makes it harder):
#define fox FOX
#define quick QUICK
#define dog DOG
the quick brown fox jumped over the lazy dog;
Your program should produce the following output:
the QUICK brown FOX jumped over the lazy DOG;
After you get your program working, use the following Perl $^I mechanism so your program modifies files in place:
If you include $^I = ".bak"
in your program before you read from <>, Perl will copy
the input file <filename> to <filename.bak> and then write
to the file <filename>. This makes it appear as if you are
modifying the file in place.
It is easier to debug your program w/o $^I mechanism because it
keeps overwriting your test files and you have to keep fixing them.
6) EXTRA CREDIT
(Weight 4) Add removal of C/C++ comments and simple includes files to
the preprocessor for problem 5. Make sure you save a copy of your
problem 5 solution.
The preprocessor removes comments. You must handle single line
//-to-end-of-line and single/multi-line /*-until-*/ comments.
You only have to include the flavor of #include that provides the full
path name in quotes and you don't have to worry about include cycles:
#include "myfile.h"