in reply to Re: Parsing text string in Perl
in thread Parsing text string in Perl

Thank you very much for your help. I would also like to print the value of ESME in the example below it would be ehttp_rkoe

ESME: ehttp_rknoe

$/ = 'Time: '; while (<DATA>) { print( (split "\n")[0] ) if /ESME_RTHROTTLED/; }

how does split work in the context of your program ?

Thanks tom

Replies are listed 'Best First'.
Re^3: Parsing text string in Perl
by 1nickt (Canon) on Sep 02, 2015 at 14:45 UTC

    Sorry, not at computer. You have records separated by "Time: ". As you loop through the record, you put the lines in an array by splitting on the new line character. The first element, or line, will contain the timestamp. You would need to loop through the elements of the array and do a regexp match, capturing the part of the line you want. But leave the existing test in there so you only loop through the record that have the interesting line.

    Update: added code example

    Here's a simplified example. I would not split into lines but use capturing regexps on the whole record, but doing the split into lines may may it simpler for you to see what's going on.

    #!/usr/bin/perl use strict; use warnings; use feature qw/ say /; use DateTime::Format::Strptime qw/ strptime /; say 'RTHROTTLED incidents:'; $/ = 'Time: '; while ( my $record = <DATA> ) { if ( $record =~ /ESME_RTHROTTLED/ ) { my @lines = split "\n", $record; # We know the time is in the first line my $time = strptime('%d/%m/%Y-%T.%3N', $lines[0])->strftime('%FT% +T.%3N'); my $ESME; foreach my $line ( @lines ) { $ESME = $1 if $line =~ /ESME:\s+(.+$)/; } say ' ' . "$time $ESME"; } }
    The way forward always starts with a minimal test.