Exercise 4 : Searching for pattern matches
Laboratory Exercise for Introduction to Perl
This assumes you have already set up your initial directory with the init-perllab command (instructions).
Checking input value (signstrict.pl)
- Change to the Lab4 directory:
cd /scratch/mylogin/PerlLab/Lab4
The directory path may be different if you installed the files in another location. - List the directory contents:
ls -l
You should see the files: INSTRUCTIONS README Solutions onlyshell.pl psout signstrict.pl - Look at the signstrict.pl file:
cat signstrict.pl
The contents of this file are as follows (note the line numbers have been added for clarity and are not in the actual file):- #!/usr/bin/perl
- # signstrict.pl - revised sign checker which requires numeric input
- use warnings;
- use strict;
- my $number;
- # Use chomp() to eliminate whitespace at beginning and end of string.
- # This will take off the newline
- # test $number to see if it fits the pattern of a number
- # may or may not start with a sign ( "-" or "+" )
- # may or may not include a decimal point
- #
- # Valid numbers: 0 0.5 .5 5.0 5. -.2 -99 +3.2 +6
- #
- # Invalid numbers: 0.5.2 3.five a0 -x
- #
- # If the entry is not a number, print an error and exit.
- if ($number < 0)
- {
- }
- elsif ($number > 0)
- {
- }
- else
- {
- }
- Edit the signstrict.pl program so it will
check the input to make sure it is a valid number, either an
integer or a decimal number. (For simplicity, don't worry about
scientific notation.)
- A number may or may not start with a sign (either - or +). Note: to include a minus sign ("-") in a character class, it must be the first character after the opening bracket ("[").
- A number may or may not include a decimal point ("."). It may not include more than one decimal point. A good way to cover multiple instances is to use alternation ("|") and grouping ("( )").
- Valid numbers include: 0 0.5 .5 5.0 5. -.2 -99 +3.2 +6
- The following are not valid numbers: 0.5.2 3.five a0 -x
See perl regular expressions, perl regular expressions tutorial, or enter
man perlre
man perlretut - If the entry is not a number, print an error and exit.
See die() or enter
perldoc -f die
Selecting input lines which fit pattern (onlyshell.pl)
- Look at the onlyshell.pl file:
cat onlyshell.pl
The contents of this file are as follows (note the line numbers have been added for clarity and are not in the actual file):- #! /usr/bin/perl
- # onlyshell.pl - read from "psout", print first line, and only lines
- # which represent shell processes. A shell is a process in which
- # the COMMAND field (last field in "ps" output) is a
- # "csh" or "bash", which has a minus sign before it:
- #
- # kjacks 21255 Sep30 0:00 -bash
- #
- # but not:
- #
- # kjacks 24536 11:47 0:00 bash
- use warnings;
- use strict;
- my $filename = 'psout';
- # open $filename for reading
- # read first line and print it
- # caution: do NOT use "print <PS>;" That will print entire file.
- # read remaining lines and print only those which match the shell pattern
- # suggestion: while() loop
- # close the file
- close PS;
- Edit the program so it will open the file
"psout
", print the first line
(which includes the column headers), and print only
those lines which represent shell processes.
A shell is a process in which the COMMAND field
(last field in ps output) is a
"csh" or "bash", which has a minus sign before it:
kjacks 21255 Sep30 0:00 -bash
but not: kjacks 24536 11:47 0:00 bash
- The output should look like:
outputUSER PID START TIME COMMAND dba1359 5881 07:08 0:00 -bash rlb3511 7168 Oct04 0:00 -bash lmkli 7415 Oct04 0:00 -bash etrufan 12337 08:37 0:00 -bash natesal 14836 09:50 0:00 -bash eosagus 15913 Oct06 0:00 -bash natesal 15943 09:58 0:00 -bash eosagus 16014 Oct06 0:00 -csh s0j3095 16994 Oct03 0:00 -bash aya3706 17849 Oct01 0:00 -bash tskim 21043 Oct06 0:00 -bash kjacks 21255 Sep30 0:00 -bash c0s2008 21998 Oct05 0:00 -bash diego07 23806 Sep30 0:00 -bash kjacks 24336 11:44 0:00 -bash qlf1582 24516 Sep29 0:00 -bash amrish 25858 12:37 0:00 -bash chao1 26154 12:43 0:00 -bash m0m391a 26283 12:46 0:00 -bash kjacks 26825 Sep29 0:00 -bash kjacks 26911 Sep29 0:00 -bash pingluo 27194 Sep29 0:00 -bash eosagus 28240 12:56 0:00 -bash bedros 32215 Oct06 0:00 -bash