in reply to Re^2: Append Next Line To Current Line
in thread Append Next Line To Current Line
First we're going to need access to previous/next lines. Instead of a foreach loop, try out a for loop so we can use index numbers on the @data array. The following snippet should get you started. Basically you now have two lines of data per each loop which will allow you to compare and append data from both.
open(FH, "error_log"); @data = <FH> for (my $index = 0; $index < $data; $index++) { my $line = @data[$index]; my $nextline = @data[$index + 1] if ($index < $data - 1); if ( ($line =~ /notice/)) { #format line $line =~ s/ /,/g; my @L1 = split(/|notice|\[|\]|,mpmstats:,|\t|rdy,|bsy, +|rd,|wr,|ka,|log,|dns,|cls,|bsy:,|in,|/, $line); $line =~ s/|notice|\[|\]|,mpmstats:,|\t|rdy,|bsy,|rd,| +wr,|ka,|log,|dns,|cls,|bsy:,|in,//g; print $line; #format nextline $nextline =~ s/ /,/g; my @L1 = split(/|notice|\[|\]|,mpmstats:,|\t|rdy,|bsy, +|rd,|wr,|ka,|log,|dns,|cls,|bsy:,|in,|/, $nextline); $nextline =~ s/|notice|\[|\]|,mpmstats:,|\t|rdy,|bsy,| +rd,|wr,|ka,|log,|dns,|cls,|bsy:,|in,//g; print $nextline; }
You'll need to take care of the ending situation (when $index is the last element of @data). I'm not entirely positive what your regex is doing, but I'm assuming that's where data is getting put into a csv format? You'll need to get at the innards of the line in order to append data from it. Hope this helps some, keep posting if you need to.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Append Next Line To Current Line
by johngg (Canon) on Jul 02, 2012 at 19:16 UTC | |
by Ransom (Beadle) on Jul 02, 2012 at 19:22 UTC |