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.

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 >
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;

Nice blog! Is your theme custom made or did you download it from somewhere?
A theme like yours with a few simple adjustements would really make my blog stand out.
Please let me know where you got your design. Many thanks

Feel free to surf to my weblog ... legit online money making jobs
Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?