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)

  1. Change to the Lab4 directory:

    cd /scratch/mylogin/PerlLab/Lab4

    The directory path may be different if you installed the files in another location.
  2. List the directory contents:

    ls -l

    You should see the files: INSTRUCTIONS README Solutions onlyshell.pl psout signstrict.pl
  3. 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):
    1. #!/usr/bin/perl
    2.  
    3. # signstrict.pl - revised sign checker which requires numeric input
    4.  
    5. use warnings;
    6. use strict;
    7.  
    8. my $number;
    9.  
    10. print "enter a number: ";
    11.  
    12. # Use chomp() to eliminate whitespace at beginning and end of string.
    13. # This will take off the newline
    14. chomp($number = <>);
    15.  
    16. # test $number to see if it fits the pattern of a number
    17. # may or may not start with a sign ( "-" or "+" )
    18. # may or may not include a decimal point
    19. #
    20. # Valid numbers: 0 0.5 .5 5.0 5. -.2 -99 +3.2 +6
    21. #
    22. # Invalid numbers: 0.5.2 3.five a0 -x
    23. #
    24. # If the entry is not a number, print an error and exit.
    25.  
    26. if ($number < 0)
    27. {
    28. print "Your number is negative\n";
    29. }
    30. elsif ($number > 0)
    31. {
    32. print "Your number is positive\n";
    33. }
    34. else
    35. {
    36. print "Your number is zero\n";
    37. }
  4. 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
  5. If the entry is not a number, print an error and exit. See die() or enter

    perldoc -f die
SOLUTION


Selecting input lines which fit pattern (onlyshell.pl)

  1. 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):
    1. #! /usr/bin/perl
    2.  
    3. # onlyshell.pl - read from "psout", print first line, and only lines
    4. # which represent shell processes. A shell is a process in which
    5. # the COMMAND field (last field in "ps" output) is a
    6. # "csh" or "bash", which has a minus sign before it:
    7. #
    8. # kjacks 21255 Sep30 0:00 -bash
    9. #
    10. # but not:
    11. #
    12. # kjacks 24536 11:47 0:00 bash
    13.  
    14. use warnings;
    15. use strict;
    16.  
    17. my $filename = 'psout';
    18.  
    19. # open $filename for reading
    20. open PS, "<$filename"
    21. or die "open($filename) $!";
    22.  
    23. # read first line and print it
    24. # caution: do NOT use "print <PS>;" That will print entire file.
    25.  
    26. # read remaining lines and print only those which match the shell pattern
    27. # suggestion: while() loop
    28.  
    29. # close the file
    30. close PS;
  2. 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
  3. The output should look like:
    output
    USER       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
    

SOLUTION