Lab 7 is the first and simplest Perl script that filters data presented on STDIN, changing all occurrences of a FROMSTRING string to a TOSTRING and outputting all input lines, changed and unchanged to STDOUT. FROMSTRING and TOSTRING can be ordinary text or PERL-compatible regular expressions.
Your Perl script shall be named filter.pl and be marked executable.
Usage: ./filter.pl ‘FROMSTRING’ ‘TOSTRING’
Example, where we read SOMEFILE and change all occurrences of FOO to BAR
cat SOMEFILE | ./filter.pl ‘FOO’ ‘BAR’
Your script should read only from STDIN and write only to STDOUT. Write error messages only to STDERR. Your script should never prompt the user or open any files.
Your script should support regular expressions and be capable of changing all occurrences of the FROMSTRING to TOSTRING . All lines are output, both changed and unchanged. exit(0) if there are no errors. It is ok for nothing on the input to be changed as they are output.
Issue a Usage message and exit(1) if there are not exactly two commandline parameters provided.
Use /usr/bin/perl on icarus as your “shebang”. use while (<STDIN>) to read each line of input Use $_ to access each line read
use s/// (the substitution operator) to change all occurrences of FROMSTRING to TOSTRING on each line
Use print to output all line on STDIN to STDOUT, whether or not the line was changed
$ARGV contains the commandline arguments. For example, $ARGV is the first argument,
$#ARGV is the index of the last item in $ARGV
Clone your private repo on github.com
In the assignment module in Canvas, find the link to the Canvas page entitled “Clone your Lab 7 Repo”, click on it and follow the instructions.
Copy your private repo down to icarus
git clone https://github.com/cowancs3030spring19/lab7-YOURGITHUBUSERNAME
Write and test filter.pl
Fire up your favorite text editor, and update the header:
• (Your name)
• Lab 7 – Perl Filter
• CS 3030 – Scripting Languages
(add your unbelievably elegant code here)
Run cucumber to check your progress
Submit your assignment code for grading
• Remember, you must push your script in your private repo to github.com to receive any points, for all assignments in this course.
git add filter.pl
git commit -m”COMMIT MESSAGE”
git push origin master
Files changed for this lab
Here is how you earn points for this assignment:
Script is named correctly and found in its proper place in your private repo
Script is executable
Script prints a “Usage:” statement and exits rc=1 if there are not exactly two
Script exits rc=0 on successful completion
Script reads from STDIN
Script writes to STDOUT unless error
Script writes error messages only to STDERR
Script properly translates the input to the output using a simple string replacement
Script supports regular expressions in FROMSTRING
Script supports regular expressions in FROMSTRING and TOSTRING