Thursday, October 26, 2006
Search, Process, Replace strings in a File using Perl
I wanted some one-line loop to read a log file containing serial numbers line-by-line, search for a serial using some regular expression, read it into a variable and then mark it as used back into the file so its not used again next time. Here is how it goes
So the file looks something like this
EVAL=SomeSerial1 #Created on Fri Oct 13 11:12:09 2006.
COM=SomeSerial2 #Created on Tue Oct 17 11:19:45 2006.
COM=SomeSerial3 #Created on Tue Oct 17 11:19:45 2006.
#Main
print &getSerialfromFile("./serialnumbers.txt", "COM=");
sub getSerialfromFile($$)
{
my $line;
my ($serials_file, $pattern) = (@_);
open(MAP, $serials_file) or die "failed to open $serials_file, $!";
my @lines = < MAP >
close(MAP);
foreach $line (@lines)
{
next unless ($line =~ s/^$pattern(.*?)\s/used$pattern$1/g) ;
# You can use the serial $1 here
}
open(MAP, ">", "./changed.txt");
print MAP @lines;
close(MAP);
}
So the file looks something like this
EVAL=SomeSerial1 #Created on Fri Oct 13 11:12:09 2006.
COM=SomeSerial2 #Created on Tue Oct 17 11:19:45 2006.
COM=SomeSerial3 #Created on Tue Oct 17 11:19:45 2006.
#Main
print &getSerialfromFile("./serialnumbers.txt", "COM=");
sub getSerialfromFile($$)
{
my $line;
my ($serials_file, $pattern) = (@_);
open(MAP, $serials_file) or die "failed to open $serials_file, $!";
my @lines = < MAP >
close(MAP);
foreach $line (@lines)
{
next unless ($line =~ s/^$pattern(.*?)\s/used$pattern$1/g) ;
# You can use the serial $1 here
}
open(MAP, ">", "./changed.txt");
print MAP @lines;
close(MAP);
}
Tuesday, October 03, 2006
Easy Error Logging in Perl via Log4Perl
Need a quick and dirty tutorial on enabling loggers in your scripts? Here you go.
Download and Install Log-Log4perl-1.06 from CPAN (May prompt you to install IO-Tty-1.07).
The following perl script will let you give you the ability to define 3 filters for your logging: Error, Warn and Info. Warings and Errors are logged to one file and Info messages to the other.
use Log::Log4perl qw(get_logger);
# Define configuration
my $conf = q(
log4perl.logger = INFO, AppInfo, AppWarn, AppError
#filter to match INFO
log4perl.filter.MatchInfo = Log::Log4perl::Filter::LevelMatch
log4perl.filter.MatchInfo.LevelToMatch = INFO
log4perl.filter.MatchInfo.AcceptOnMatch = true
#filter to match Warn
log4perl.filter.MatchWarn = Log::Log4perl::Filter::LevelMatch
log4perl.filter.MatchWarn.LevelToMatch = WARN
log4perl.filter.MatchWarn.AcceptOnMatch = true
# filter to match Error
log4perl.filter.MatchError = Log::Log4perl::Filter::LevelMatch
log4perl.filter.MatchError.LevelToMatch = ERROR
log4perl.filter.MatchError.AcceptOnMatch = true
# Info Appender
log4perl.appender.AppInfo = Log::Log4perl::Appender::File
log4perl.appender.AppInfo.filename = detail.log
log4perl.appender.AppInfo.layout = PatternLayout
log4perl.appender.AppInfo.layout.ConversionPattern = %d %p> %F{1}:%L %M - %m%n
log4perl.appender.AppInfo.Filter = MatchInfo
# Warn Appender
log4perl.appender.AppWarn = Log::Log4perl::Appender::File
log4perl.appender.AppWarn.filename = result.log
log4perl.appender.AppWarn.layout = PatternLayout
log4perl.appender.AppWarn.Filter = MatchWarn
# Error Appender
log4perl.appender.AppError = Log::Log4perl::Appender::File
log4perl.appender.AppError.filename = detail.log
log4perl.appender.AppError.layout = PatternLayout
log4perl.appender.AppError.layout.ConversionPattern = %d %p> %F{1}:%L %M - %m%n
log4perl.appender.AppError.Filter = MatchError
);
# Initialize logging behaviour
Log::Log4perl->init( \$conf );
my $logger = get_logger("issa::test");
$logger->info("License file installed successfully");
Download and Install Log-Log4perl-1.06 from CPAN (May prompt you to install IO-Tty-1.07).
The following perl script will let you give you the ability to define 3 filters for your logging: Error, Warn and Info. Warings and Errors are logged to one file and Info messages to the other.
use Log::Log4perl qw(get_logger);
# Define configuration
my $conf = q(
log4perl.logger = INFO, AppInfo, AppWarn, AppError
#filter to match INFO
log4perl.filter.MatchInfo = Log::Log4perl::Filter::LevelMatch
log4perl.filter.MatchInfo.LevelToMatch = INFO
log4perl.filter.MatchInfo.AcceptOnMatch = true
#filter to match Warn
log4perl.filter.MatchWarn = Log::Log4perl::Filter::LevelMatch
log4perl.filter.MatchWarn.LevelToMatch = WARN
log4perl.filter.MatchWarn.AcceptOnMatch = true
# filter to match Error
log4perl.filter.MatchError = Log::Log4perl::Filter::LevelMatch
log4perl.filter.MatchError.LevelToMatch = ERROR
log4perl.filter.MatchError.AcceptOnMatch = true
# Info Appender
log4perl.appender.AppInfo = Log::Log4perl::Appender::File
log4perl.appender.AppInfo.filename = detail.log
log4perl.appender.AppInfo.layout = PatternLayout
log4perl.appender.AppInfo.layout.ConversionPattern = %d %p> %F{1}:%L %M - %m%n
log4perl.appender.AppInfo.Filter = MatchInfo
# Warn Appender
log4perl.appender.AppWarn = Log::Log4perl::Appender::File
log4perl.appender.AppWarn.filename = result.log
log4perl.appender.AppWarn.layout = PatternLayout
log4perl.appender.AppWarn.Filter = MatchWarn
# Error Appender
log4perl.appender.AppError = Log::Log4perl::Appender::File
log4perl.appender.AppError.filename = detail.log
log4perl.appender.AppError.layout = PatternLayout
log4perl.appender.AppError.layout.ConversionPattern = %d %p> %F{1}:%L %M - %m%n
log4perl.appender.AppError.Filter = MatchError
);
# Initialize logging behaviour
Log::Log4perl->init( \$conf );
my $logger = get_logger("issa::test");
$logger->info("License file installed successfully");