mrbbq has asked for the wisdom of the Perl Monks concerning the following question:

Hey Monks...I am in need of wisdom:

I am writting this script to see why a given tape goes bad and I am stuck on what seems to be real basic.

Given the below code I have a few questions.

  1. when i open the HANDLE, the one that is commented out doesnt work but i think it should
  2. What would make this work?
  3. At the bottom you will see an example of the logfile I am trying to pharse. I want to find the frozen tapes and want to know why they were frozen, what is the better way to do this?


$date =`/bin/date +'%m%d%y'`; #print $date; my $LOG = "log.$date"; print "$LOG\n"; open (MYLOG, ">/export/home/zmeekins/frozen.txt") or die "Can not open logfile: $!"; #open HANDLE, "/usr/bin/cat /usr/openv/netbackup/logs/bptm/$LOG # | grep FREEZING |") or die "can't get the list"; open (HANDLE, "/usr/bin/cat /usr/openv/netbackup/logs/bptm/log.* | grep FREEZING | +") or die "can't get the list"; # New idea while (<HANDLE>){ chomp $_; ($timestamp, $dummy, $dummy, $dummy, $freezing, $dummy, $dummy, $tapeid, $dummy0, $dummy1, $dummy2, $dummy3, $dummy4, $dummy5, $dummy6, $dummy7, $dummy8, $dummy9, $dummy10, $dummy11, $dummy12) = split; print "I am $freezing $tapeid $dummy0 $dummy1 $dummy2 $dummy3 $dummy4 +$dummy5 $dummy6 $dummy7 $dummy8 $dummy9 $dummy10 $dummy11 $dummy12 \n +"; } #10:46:50.704 [18228] <16> write_backup: FREEZING media id ST6196, it +is unmountable and cannot be used for backups #23:45:21.081 [14993] <8> write_backup: FREEZING media id ST6816, it c +ontains NetBackup database backup data and cannot be used for backups #23:37:51.506 [26186] <8> check_error_history: FREEZING media id ST067 +0, it has had at least 3 errors in the last 12 hour(s)

Code tags added by davido.

Replies are listed 'Best First'.
Re: Monks to the rescue
by graff (Chancellor) on Jan 28, 2005 at 05:17 UTC
    When you use a back-ticked command line to assign a string to a perl variable, the string includes the final line-feed character that was printed at the end by the shell command. So if you plan to use the string as a file name in an open statement, you need to "chomp" it:
    my $date =`/bin/date +'%m%d%y'`; chomp $date; my $LOG = "log.$date"; # another way to do that is to use perl internals instead of a sub-she +ll: { my ($d,$m,$y)=(localtime)[3..5]; my $perldate = sprintf "%02d%02d%02d", $m+1, $d, $y-100; die "This should never happen: $perldate != $date" if $perldate ne $date; }
    As for your question 3, I'm not quite sure about your intention, but I think the best way to start is by using a regex match and capture:
    while (<HANDLE>) { next unless ( /FREEZING media id (.*)/ ); print "I am FREEZING $1\n"; }
    (BTW, when using date strings in file names, I like to arrange them as "%y%m%d", so they sort nicely.)

    update: Forgot to mention: when using a regex match in the while loop as shown above, you don't need to open a pipeline that runs the data through a separate "grep" process -- perl does the grep for you, by ignoring the lines that don't match the regex.

      Thanks! That was a big help! I am doing the "your the man" dance around my cubicle as I type. Thanks again.
Re: Monks to the rescue
by runrig (Abbot) on Jan 28, 2005 at 00:53 UTC
    Your file name is being interpreted as a command whose output gets piped to grep. You want "grep FREEZING file-name|" not "file-name | grep FREEZING |".

    Update: Now that there are code tags in the original post, I see that you're using "cat file-name...", so nevermind (though it is a "useless use of cat").