Link to home
Start Free TrialLog in
Avatar of areyouready344
areyouready344Flag for United States of America

asked on

how to execute multiple search and replace regular expressions using against the same log files Perl?

What's the best way to execute multiple search and replace regular expressions for a single exs.log file for formating using Perl?

For example, I have a log file call exs.log and would like to run the following two search and replace lines below against it. In reality, I have more than two search and replace lines but knowing how to do two search and replace lines would be good enough

$_ =~ s/(^\@.*?)\n(or\s+.*)/$1$2/ms;
s{^(\@test.*?$os(?:\s*\([^)]+\))?\s*\n)(?!hardware-type)}{$1Undefined Hardware type\n}ms;

Here's the code I started but not completed..

#!/usr/bin/perl

use warnings;
use strict;

my @log_files = qw(esx.log esx.log2 esx.log3);

foreach (@log_files)
{
   open 'Input_log','<',"/etc/log/$_";
   open 'Output_log','>>',"/home/test_logs";
   
   while(<Input_log>)
   {
         I'm at a lost from this point on... I'm not sure how to take the output of a log file for a search and replace and use it as an input_log to another search and replace line.
   }

}
ASKER CERTIFIED SOLUTION
Avatar of wilcoxon
wilcoxon
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
However, the regular expressions you want to use won't work with the skeleton code you had as the code you have will loop over the input log line-by-line (whereas both regexes are looking for multi-line matches).  If you set $/ then you can get multiple lines (guessing this will be $/='__Data__').

Also, you need to define $os for the above code to work.
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