Newlines and deprecations

J

jesse

I have a perl script that parses a file of backup failures. I have it
print what failed and how many times it's failed. The log file lines
look like this:

c3devweb3 C:\\
c3devweb3 D:\\

This is the name of the server and what failed. The scripts seems to
be working okay but there are some problems in the output. Here is
the script.

#!/usr/bin/perl
#
use:strict;
use warnings;
my $file = "/export/home/jhardy/failed";
my %failures;

open(FAILED, "<", $file) or die "$!\n";
while (<FAILED>) {
s/#.*//;
next if /^(\s)*$/;
my $failure = ( split,(/ /, $_) );
$failures{$failure}++;
}
close(FAILED);
foreach $failure ( keys %failures) {
print "The backup of $failure has failed $failures{$failure}
times this month\n";
}

The output I get is this:

The backup of c3duoraint1-bkup /
has failed 2 times this month
The backup of c3duoraint1-bkup /export/home
has failed 2 times this month

The output needs to be on one line ie;
The backup of c3duoraint1-bkup / has failed 2 times this month.

Can someone tell me what I'm doing wrong. I am also getting an error
on the split it says the way I'm using it is deprecated and I'm not
sure what I'm doing wrong there either.

Thanks,
Jesse
 
P

Paul Lalli

I have a perl script that parses a file of backup failures. I have it
print what failed and how many times it's failed. The log file lines
look like this:

c3devweb3 C:\\
c3devweb3 D:\\

This is the name of the server and what failed. The scripts seems to
be working okay but there are some problems in the output. Here is
the script.

#!/usr/bin/perl
#
use:strict;
use warnings;
my $file = "/export/home/jhardy/failed";
my %failures;

open(FAILED, "<", $file) or die "$!\n";
while (<FAILED>) {

You forgot to chomp. That's why you have a newline in your output.

chomp;
s/#.*//;
next if /^(\s)*$/;
my $failure = ( split,(/ /, $_) );

You have two different errors here that are working in conjunction to
produce almost-correct results. For one, split should not have a
comma before its first argument. For two, it's using split in a
depreciated context that's depreciated. Fortunately, because you
erroneously put that comma there, you're not getting the return value
of split called in a scalar context stored into $failure. Instead,
you're getting $_ itself - the whole line. Now because you're not
complaining that you're getting the whole line of the file rather than
just the part after the spaces, I can only assume that you really have
no need to split at all. So just assign $failure to $_:
my $failure = $_;

If you really did want to split the line and use just the last part of
it, then call split in a list context, and get the last element of the
returned list:

my $failure = (split / /, $_)[0];


Paul Lalli
 
M

marora

I have a perl script that parses a file of backup failures. I have it
print what failed and how many times it's failed. The log file lines
look like this:

c3devweb3 C:\\
c3devweb3 D:\\

This is the name of the server and what failed. The scripts seems to
be working okay but there are some problems in the output. Here is
the script.

#!/usr/bin/perl
#
use:strict;
use warnings;
my $file = "/export/home/jhardy/failed";
my %failures;

open(FAILED, "<", $file) or die "$!\n";
while (<FAILED>) {
s/#.*//;
next if /^(\s)*$/;
my $failure = ( split,(/ /, $_) );
$failures{$failure}++;}

close(FAILED);
foreach $failure ( keys %failures) {
chomp($failure); # should do the trick
 
K

kens

I have a perl script that parses a file of backup failures. I have it
print what failed and how many times it's failed. The log file lines
look like this:
c3devweb3 C:\\
c3devweb3 D:\\
This is the name of the server and what failed. The scripts seems to
be working okay but there are some problems in the output. Here is
the script.
#!/usr/bin/perl
#
use:strict;
use warnings;
my $file = "/export/home/jhardy/failed";
my %failures;
open(FAILED, "<", $file) or die "$!\n";
while (<FAILED>) {

You forgot to chomp. That's why you have a newline in your output.

chomp;
s/#.*//;
next if /^(\s)*$/;
my $failure = ( split,(/ /, $_) );

You have two different errors here that are working in conjunction to
produce almost-correct results. For one, split should not have a
comma before its first argument. For two, it's using split in a
depreciated context that's depreciated. Fortunately, because you
erroneously put that comma there, you're not getting the return value
of split called in a scalar context stored into $failure. Instead,
you're getting $_ itself - the whole line. Now because you're not
complaining that you're getting the whole line of the file rather than
just the part after the spaces, I can only assume that you really have
no need to split at all. So just assign $failure to $_:
my $failure = $_;

If you really did want to split the line and use just the last part of
it, then call split in a list context, and get the last element of the
returned list:

my $failure = (split / /, $_)[0];

Paul Lalli

Looks like a typo - that will get the first element.
The following would get the last:

my $failure = (split / /, $_)[-1];

Ken
 
J

jesse

I have a perl script that parses a file of backup failures. I have it
print what failed and how many times it's failed. The log file lines
look like this:
c3devweb3 C:\\
c3devweb3 D:\\
This is the name of the server and what failed. The scripts seems to
be working okay but there are some problems in the output. Here is
the script.
#!/usr/bin/perl
#
use:strict;
use warnings;
my $file = "/export/home/jhardy/failed";
my %failures;
open(FAILED, "<", $file) or die "$!\n";
while (<FAILED>) {

You forgot to chomp. That's why you have a newline in your output.

chomp;
s/#.*//;
next if /^(\s)*$/;
my $failure = ( split,(/ /, $_) );

You have two different errors here that are working in conjunction to
produce almost-correct results. For one, split should not have a
comma before its first argument. For two, it's using split in a
depreciated context that's depreciated. Fortunately, because you
erroneously put that comma there, you're not getting the return value
of split called in a scalar context stored into $failure. Instead,
you're getting $_ itself - the whole line. Now because you're not
complaining that you're getting the whole line of the file rather than
just the part after the spaces, I can only assume that you really have
no need to split at all. So just assign $failure to $_:
my $failure = $_;

If you really did want to split the line and use just the last part of
it, then call split in a list context, and get the last element of the
returned list:

my $failure = (split / /, $_)[0];

Paul Lalli- Hide quoted text -

- Show quoted text -

I have a major error in my logic and wanted to know if you could give
me some pointers. I need to count the number of occurences for each
failure in the file but if the failure didn't happen the day of the
report I want to ignore any of those failures in the master file until
the failure happens again. I don't think I can really use a regular
expression because I'm not looking for a specific word. I have 400
clients that I backup on a nightly basis and it could be any of those
400.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top