Domanda

I would like to know how to extract a part of line from a file in perl. I have a log file from which I want to extract few meaningful information through a perl script. I was able to get the whole line which i was looking for, but i need only a part of that line.

Perl script(I have used):

#!/usr/bin/perl
use strict;
use warnings;

my $file='F:\3Np_RoboSitter\perl pgm\input.txt';

open my $fh, "<", $file or die $!;

print "************************************************************\n";
print "DC status:\n\n";

while (<$fh>) {
        print if /DC messages Picked/ .. /DC messages Picked from the Queue/;
}

print "\n************************************************************\n\n";
close ($fh); 

Input file:

adfaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaadfafafafqdrareeaf
2014-02-14 00:18:04,840 1754897056   INFO    ApplicationService    aadfafa123    ApplicationService    ApplicationServiceCustomerID     ApplicationServiceSessionToken    Parse of XML started. |HostName=AAAAAA|TimeStamp=2014-02-14 00:16:39.044|Message=OUT;submitApplications.SubmitApplicationBatchProcess;Total 1311 DC messages Picked from the Queue.|Detail=<XMLNSC><LogMessage><messageText>Total 1311 DC messages Picked from the Queue.</messageText></LogMessage></XMLNSC>    
dafafafzcvzvsfdfafafffffffffffffffffffffffff

Output:

************************************************************
DC status:

2014-02-14 00:18:04,840 1754897056   INFO    ApplicationService    aadfafa123
 ApplicationService    ApplicationServiceCustomerID     ApplicationServiceSessio
nToken    Parse of XML started. |HostName=AAAAAA|TimeStamp=2014-02-14 00:16:39.0
44|Message=OUT;submitApplications.SubmitApplicationBatchProcess;Total 1311 DC me
ssages Picked from the Queue.|Detail=<XMLNSC><LogMessage><messageText>Total 1311
 DC messages Picked from the Queue.</messageText></LogMessage></XMLNSC>
************************************************************

Desired Output:

2014-02-14 00:18:04
Total 1311 DC messages Picked from the Queue. *(Which is between <messagetext> tag)* 

Team, kindly provide ur valuable suggestion to achieve this in your free moment!...

È stato utile?

Soluzione

It always based on the input. Your input is not well formatted (not fixed length, not CSV) so the easiest would be the regexp method.

while (my $line = <$fh>){
  my ($date) = split(/,/,$line,2);
  if ($line =~ s!<messageText>(.+?)</messageText>!!is){
     print "$date\n$1\n";
  }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top