Link to home
Start Free TrialLog in
Avatar of rleyba828
rleyba828Flag for Australia

asked on

Need some assistance modifying a PERL script

Hi Team,

   I would just like to seek some help modifying a script already created for me by one of our valuable members (wilcoxon)

  The script basically parses a raw syslog file and breaks them into different fields enclosed by quotes.  The original script is as follows

#!/usr/bin/perl
use strict;
use warnings;

# you need to replace the files in the open lines with the real ones
open IN, 'Syslog' or die "could not read syslogs: $!";
open OUT, '>syslog.csv' or die "could not write syslog.csv: $!";
while (<IN>) {
    chomp;
    s{"}{'}g; # replace " with ' just to make life easier
    if (m{^(\w+)\s+(\d+)\s+([\d:]+)\s+(\S+)\s+(.*)$}) {
        print OUT join(',', map { '"' . $_ . '"' } $1, $2, $3, $4, $5), "\n";
    } else {
        die "could not parse line $.:\n$_";
    }
}

Open in new window


the output of the script would look like this
"Sep","1","21:52:33","10.22.14.8","local/LB1 notice: 011ae020:5: Connection in progress to 10.9.8.8  "
"Sep","1","21:52:33","10.4.174.67","local/LB2 notice  011ae020:5: Connection in progress to 10.9.8.9"

We now have a new requirement to have the Months field changed to their two digit nnumeric equivalent, e.g. 01 for Jan, 02 for Feb....09 For Sep.....etc.  Also, the double quotes should be omitted.

Using the same two lines above, the final output should be like this:


09, 1, 21:52:33, 10.22.14.8, local/LB1 notice: 011ae020:5: Connection in progress to 10.9.8.8  
09, 1, 21:52:33, 10.4.174.67, local/LB2 notice  011ae020:5: Connection in progress to 10.9.8.9

May I just request help on how the original script should be modified to achieve my intended outcome?

Thanks very much.


Avatar of mankowitz
mankowitz
Flag of United States of America image

First, to get rid of the quotes, remove the map function
Second... use this hash to map month names to numbers

%mon2num = qw(
  jan 1  feb 2  mar 3  apr 4  may 5  jun 6  jul 7  aug 8  sep 9  oct 10 nov 11 dec 12
);

 
#!/usr/bin/perl
use strict;
use warnings;

# you need to replace the files in the open lines with the real ones
open IN, 'Syslog' or die "could not read syslogs: $!";
open OUT, '>syslog.csv' or die "could not write syslog.csv: $!";
while (<IN>) {
    chomp;
    s{"}{'}g; # replace " with ' just to make life easier
    if (m{^(\w+)\s+(\d+)\s+([\d:]+)\s+(\S+)\s+(.*)$}) {
        print OUT join(',', $mon2num{lc $1}, $2, $3, $4, $5), "\n";
    } else {
        die "could not parse line $.:\n$_";
    }
}

Open in new window

Avatar of rleyba828

ASKER

Hi mankowitz,

   Thanks for this......sorry I am not a linux expert....but where do you declare the %mon2num variable?

SOLUTION
Avatar of FishMonger
FishMonger
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Assigning to a hash in that manor is poor style.  It's better to use the fat comma, and you forgot the 'my' keyword.  Also, the OP wants 2 digit format for the month, which you're not accounting for the months 1..9.

My example has a minor typo on opening the $syslog_fh filehandle.  It should read as:
open my $syslog_fh, '<', 'Syslog' or die "could not read syslogs: $!";

Open in new window

@fish

1. Assigning a hash in a way that works is always good style in perl.
2. For a script that's only 20 lines, I don't really need to worry about variable scope. I don't think the my keyword will make much of a difference here.
3. You're right about the 2 digit v. 3 digit format.
4. You wrote manor, but I think you mean manner. In perl, as in human languages, spelling is important.
#!/usr/bin/perl
use strict;
use warnings;

%mon2num = qw(jan 01 feb 02 mar 03 apr 04 may 05 jun 06 jul 07 aug 08 sep 09 oct 10 nov 11 dec 12);

# you need to replace the files in the open lines with the real ones
open IN, 'Syslog' or die "could not read syslogs: $!";
open OUT, '>syslog.csv' or die "could not write syslog.csv: $!";
while (<IN>) {
    chomp;
    s{"}{'}g; # replace " with ' just to make life easier
    if (m{^(\w+)\s+(\d+)\s+([\d:]+)\s+(\S+)\s+(.*)$}) {
        print OUT join(',', $mon2num{lc $1}, $2, $3, $4, $5), "\n";
    } else {
        die "could not parse line $.:\n$_";
    }
}

Open in new window

mankowitz,

1) I never said that your hash assignment wouldn't work, but it is poor style, in part because it's fairly easy to use an odd number of elements and if you have a lengthy assignment, it can be difficult to determine what is missing.

2) In this case scoping is not an issue, but 20 line scripts often get expanded or inserted into much larger scripts where scoping will be an issue.

3) The my keyword is important and will make a big difference here.  Based on your assertion that it wouldn't I can only assume that you didn't test your script.  Did you know that it won't compile?

4) The use of bareword filehandles is discouraged in today's Perl coding standards.  It is best practice to use a lexical var for the filehandle and the 3 arg form of open.

5) It's also best practice to keep line lengths below 80 characters, some would say no more than 72.

6) Thank you for pointing out my spelling error.  I try to learn from my mistakes.
@fish-

touche.

thanks for keeping me humble
Hi Team,

  Sorry for the delay in replying and  thanks very much for your tips.  My final script below is based on your suggestions above.

#!/usr/bin/perl
use strict;
use warnings;

my %mon2num = qw(
  jan 01  feb 02  mar 03  apr 04  may 05  jun 06  jul 07  aug 08  sep 09  oct 10 nov 11 dec 12
);

# you need to replace the files in the open lines with the real ones
open IN, 'Syslog' or die "could not read syslogs: $!";
open OUT, '>syslog.csv' or die "could not write syslog.csv: $!";
while (<IN>) {
    chomp;
    s{"}{'}g; # replace " with ' just to make life easier
    if (m{^(\w+)\s+(\d+)\s+([\d:]+)\s+(\S+)\s+(.*)$}) {
        print OUT join(',', $mon2num{lc $1}, $2, $3, $4, $5), "\n";
    } else {
        die "could not parse line $.:\n$_";
    }
}

Open in new window