in reply to Log Parsing
Rather than iterating through an array to find a match like this
foreach $line (<LOGFILE1>) { chomp($line); foreach $childid (@Task_id) {
build a hash and use the keys to match
poj#!/usr/bin/perl use strict; my %data = (); my @id = (); my $reqid = '8274'; my $infile = 'worker.log'; my $outfile = 'data.CSV'; open IN,'<',$infile or die "$!"; # input while (<IN>) { chomp; next unless /Task:id=(\d+)/; my $taskid = $1; my (undef,$timestamp,undef) = split /\s+/,$_,3; if (/(Started|Done).+topic (\d+_(\d+)_.+)/){ $data{$3}{$1} = $timestamp; $data{$3}{'Topic'} = $2; } while ( /\{(\d+)-SUCCESSFUL\}/g ){ push @id,$1 if ($taskid eq $reqid); } } close IN; # output open OUT,'>',$outfile or die "$!"; my @cols = qw(Topic Started Done); printf OUT "%s,%s,%s\n",@cols; for my $id (sort @id){ if (exists $data{$id}){ printf OUT "%s,%s,%s\n", map { $data{$id}{$_} } @cols; } else { print OUT "$id - no data\n"; } } close OUT;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Log Parsing
by piyushmnnit06 (Novice) on Apr 03, 2017 at 05:56 UTC | |
by poj (Abbot) on Apr 03, 2017 at 06:08 UTC | |
by piyushmnnit06 (Novice) on Apr 03, 2017 at 06:29 UTC | |
by poj (Abbot) on Apr 03, 2017 at 06:58 UTC | |
by piyushmnnit06 (Novice) on Apr 03, 2017 at 07:35 UTC | |
|