Looks like you only need to read the file once:

use strict; use warnings; my $fInName = "d:/Log.txt"; my $fOutName = "d:/Output/op.txt"; open my $fIn, '<', $fInName or die "Can't open '$fInName': $!\n"; open my $fOut, '>', $fOutName or die "Can't create '$fOutName': $!\n"; my %ids; while (<$fIn>) { my ($date, $id, $keyword) = /\[(.+?)\] .* \[(.+?)\] .* \[[^]]+\] \s+ (.*) /x or next; push @{$ids{$id}}, [$date, $id, $keyword]; } for my $id (sort keys %ids) { for my $entry (@{$ids{$id}}) { my ($date, $id, $keyword) = @$entry; if ($keyword eq "Orchestration Started") { print $fOut "$date,$id,$keyword \n"; next; } if ($keyword =~ /^Input Message/) { print $fOut "$date,$id," . "Input Message to P5 \n"; next; } if ($keyword =~ /^TP Service Request/) { print $fOut "$date,$id," . "Service Request \n"; next; } if ($keyword =~ /^P5 Move request :/) { print $fOut "$date,$id," . "Move request \n"; next; } if ($keyword =~ /^ProcessName:/) { my $mess = substr $keyword, 12; print $fOut "$date,$id,$mess \n"; next; } if ($keyword =~ /^Process Message :/) { my $mess = substr $keyword, 17; print $fOut "$date,$id,$mess \n"; next; } } }

Completely untested because you didn't supply any sample data, but there's a fair chance it'll just work.

Note use of strictures, lexical file handles, three parameter open and informative open failure diagnostics. Also note that variables are declared where they are first needed so that scope is managed correctly and accidental reuse is more likely to be noticed.

Oh, and the regex only needs to be given once.

Perl is the programming world's equivalent of English

In reply to Re: very slow processing by GrandFather
in thread very slow processing by sandy105

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.