regex - Merging mutiple log files by timestamp in 2nd column -
i have multiple logfiles same day . want merge them in single 1 based on timestamp in logs using perl
script.
log-1.log
2014-06-02 21:54:38,805 info com.homemaneger [executor:thread-19]: myinfo started myid=test-401406 2014-06-02 21:56:27,358 info com.homemaneger [executor:thread-13]: homemaneger: populatemyinfo completed id = test-401406, 2014-06-02 21:59:32,358 info com.homemaneger [executor:thread-17]: myinfo completed myid=test-401405
log-2.log:
2014-06-02 21:56:27,295 info com.homemaneger.mycommand [proxy:proxyservice:tcpworker:2]: mycommand::processing reqest[ab:myinfo] obj(collection [id={005a004a5b0f9}, ] ) client(poffbobj [id={xxxxxx-e8f5-11d5-yyy-0002b33d9d0c}, meta={}, fields=[xxx]] ) 2014-06-02 21:58:27,310 info com.homemaneger.updatemyinfotask
merged log:
2014-06-02 21:54:38,805 info com.homemaneger [executor:thread-19]: myinfo started myid=test-401406 2014-06-02 21:56:27,295 info com.homemaneger.mycommand [proxy:proxyservice:tcpworker:2]: mycommand::processing reqest[ab:myinfo] obj(collection[id={005a004a5b0f9}, ] ) client(poffbobj [id={xxxxxx-e8f5-11d5-yyy-0002b33d9d0c}, meta={}, fields=[xxx]] ) 2014-06-02 21:56:27,358 info com.homemaneger [executor:thread-13]: homemaneger: populatemyinfo completed id = test-401406, 2014-06-02 21:56:32,358 info com.homemaneger [executor:thread-17]: myinfo completed myid=test-401405 2014-06-02 21:58:27,310 info com.homemaneger.updatemyinfotask
i new perl
,any appreciated.
the following script can process number of log files , output merged log file.
it load them memory size factor:
#!/usr/bin/perl use warnings; use strict; die "usage: perl $0 log1 log2 > merged.log\n" if !@argv; @lines; while (<>) { if (/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/) { push @lines, $_; } else { $lines[-1] .= $_; } } print sort @lines;
if memory factor, you'll need official merge sort.
the following adapted perl merge sort
march of year:
use strict; use warnings; use autodie; die "usage: perl $0 log1 log2 > merged.log\n" if !@argv; # initialize file handles @fhs = map {open $fh, '<', $_; $fh} @argv; # first line of each file @data = map {scalar <$_>} @fhs; # loop while next line exists while (@data) { # pull out next entry. $index = (sort {$data[$a] cmp $data[$b]} (0..$#data))[0]; print $data[$index]; # fill in next data @ index. while (defined($data[$index] = readline $fhs[$index])) { last if $data[$index] =~ /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/; print $data[$index]; } # end of file if (! defined($data[$index]) { splice @fhs, $index, 1; splice @data, $index, 1; } }
Comments
Post a Comment