perl - How to Append Next Line to Current Line -
i struggling find way append next line current line if timestamp matches. here code far:
open(fh, "error_log:); @data = <fh> foreach $line (@data) { if ( ($line =~ /notice/)) { $line =~ s/ /,/g; @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;
note printed see output. output following:
wed,jun,13,10:40:35,2012,758,42,0,29,11,0,0,2 wed,jun,13,10:40:35,2012,29,mod_was_ap22_http.c wed,jun,13,10:41:35,2012,761,39,0,34,5,0,0,0 wed,jun,13,10:41:35,2012,34,mod_was_ap22_http.c wed,jun,13,10:42:35,2012,769,31,0,22,6,0,0,3 wed,jun,13,10:42:35,2012,22,mod_was_ap22_http.c wed,jun,13,10:43:35,2012,754,46,0,29,17,0,0,0
i number (29 on 2nd line) placed in csv form after others on first line corresponding timestamp. rest of line can deleted. if line has nothing below (ex. last line) append zero. thank help.
here part of input data requested:
[wed jun 13 01:41:24 2012 [error [client 10.119.84.9 file not exist: /ebiz/b2b/ihs70prd/htdocs/offline.b2bonline.html [wed jun 13 01:41:25 2012 [error [client 10.119.84.9 file not exist: /ebiz/b2b/ihs70prd/htdocs/offline.b2bonline.html [wed jun 13 01:41:25 2012 [error [client 10.119.84.8 file not exist: /ebiz/b2b/ihs70prd/htdocs/offline.b2bonline.html [wed jun 13 01:41:28 2012 [error [client 10.119.116.8 file not exist: /ebiz/b2b/ihs70prd/htdocs/offline.b2bonline.html [wed jun 13 01:41:28 2012 [error [client 10.119.84.8 file not exist: /ebiz/b2b/ihs70prd/htdocs/offline.b2bonline.html [wed jun 13 01:41:34 2012 [notice mpmstats: rdy 786 bsy 14 rd 0 wr 11 ka 3 log 0 dns 0 cls 0 [wed jun 13 01:41:34 2012 [notice mpmstats: bsy: 11 in mod_was_ap22_http.c [wed jun 13 01:41:34 2012 [error [client 10.119.84.9 file not exist: /ebiz/b2b/ihs70prd/htdocs/offline.b2bonline.html [wed jun 13 01:41:35 2012 [error [client 10.119.84.9 file not exist: /ebiz/b2b/ihs70prd/htdocs/offline.b2bonline.html
your input strange. usually, see matched square brackets.
that aside, want this:
# assumes have perl 5.10 or autodie installed: failures in open, readline, # or close die automatically use autodie; # chunks of input ignore, see below... %ignorables = map { $_ => 1 } qw( [notice mpmstats: rdy bsy rd wr ka log dns cls bsy: in ); # 3-arg open safer 2, lexical $fh better global fh glob open $error_fh, '<', 'error_log'; # iterates on lines in file, putting each $_ while (<$error_fh>) { # worry lines containing [notice if (/\[notice/) { # split line fields, separated spaces, skip %ignorables @line = grep { not defined $ignorables{$_} } split /\s+/; # more cleanup s/^\[//g @line; # remove [ [foo # output line print join(",", @line); # assuming second line has "in" in it, # whatever condition fits data... if (/\bin\b/) { # \b matches word edges, e.g., avoids matching "glint" print "\n"; } else { print ","; } } } close $error_fh;
i did not compile this, can't guarantee didn't typo somewhere.
the key here first print
without newline, end comma. then, add newline when detect second line.
you instead declare @line
outside loop , use accumulate fields until need output them newline on end.
Comments
Post a Comment