http://www.perlmonks.org?node_id=980443
superwombat has asked for the wisdom of the Perl Monks concerning the following question:

So, I've been working on a simple program to help me parse some log files. I have a regular expression that searches each line of the file, if it matches my search string, then it returns the date and timestamp, as well as the result (numerical value) at the end of the log file line.

Here's a sample line from the file I'm parsing

20120704 00:05:53.46;CmdTask(0);EV;FBLdxPreAlignment rtn=0, Yoffset=-4278

my $tags[0] is set to "FBLdxPreAlignment"

Here's my regex

if ($_=~/(\d+ \d+:\d+:\d+.\d+).*$tags[0].*\=(\-?\d+.?\d*).*/){

When I just print the results, either to the console or to a file, it works perfectly.

print "$1,$2\n";

returns "20120704 00:05:53.46,-4278"

What I need, is for it to print the results, several commas, then a newline. This is so I can have separate columns of data in an output CSV for more than one search string at a time. When I change the code to print out a string of trailing commas, the following output happens.

print "$1,$2,,,,,\n";

returns ",,,,,704 00:05:53.46,-4278"

I'm not super experienced at regexs, so I thought maybe $1 $2 were somehow being modified on the fly. I tried saving them to a variable as soon as they were generated

my $data = "$1,$2\n"; chomp $data; print "$data\n"; print "$data,,,,,\n";

returns:

"20120704 00:05:53.46,-4278"

",,,,,704 00:05:53.46,-4278"

Now, you'll notice I added a newline and then chomped it. If I don't have the newline on the end, I get no output at all.

print "$1,$2";

Results: blank

The logfiles I'm working with are from a machine running VMS. I think the issues I'm having may be related to different formatting of the data (newlines characters or something). If I copy several lines from the file and save it as a .log file on my own computer, it works as expected (the comma's go at the end where I want them) I'll go ahead and post the entirety of my current code here at the end, in case there's some other error I've made that I'm missing.

foreach (@files){ $logfile=$_; print "$_\n"; print OUTFILE "Time,$tags[0],$tags[1],$tags[2],$tags[3],$tags[4]\n +"; # open LOGFILE, "$logfile"; while (<LOGFILE>){ chomp $_; if ($_=~/(\d+ \d+:\d+:\d+.\d+).*$tags[0].*\=(\-?\d+.?\d*).*/){ my $data = "$1,$2\n"; print "$data"; chomp $data; print "$data,,,,,\n"; print "$data"; } } close LOGFILE; }