Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Matching numeric digits in strings

by flemi_p (Novice)
on Jan 10, 2007 at 11:59 UTC ( [id://593887]=perlquestion: print w/replies, xml ) Need Help??

flemi_p has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

Thanks for your help before I start.

I need to use Perl again, having used it very elegently and successfully ages ago to interrogate a log file. In this case the log file is <INFILE>. I want to match each line in this file with each reqid in <REQIDS_FILE>. I iterate over the <REQIDS_FILE> and for each reqid I then iterate over <INFILE>, outputting any line matching the reqid. When a matched line also contains an SQL SELECT statement, I also need to output the next 3 lines in <INFILE>. The issue is checking for the existence of reqid in the line from <INFILE>. As you can see from the code, I've tried some stuff but I never get to the MATCHED statement.

The logic is not correct nor is the code complete. The reason for this is that I suspect what I'm trying to do is second nature to many of you. I can post compiled code if you need it.

Any help is much appreciated. Thanks again.
<INFILE> 15:53:42.332 Dbg 10741 'App: 12' has put request id='834531' into queu +e 15:53:42.332 Dbg 10737 Forwarding request '834531' from 'App: 12' to ' +./dbclient_oracle: 12.1' 15:53:43.908 Dbg 10749 Forwarding response '834531.1' from './dbclient +_oracle: 12.1' to 'App: 12' 15:53:43.908 Dbg 10749 Forwarding response '834531.2' from './dbclient +_oracle: 12.1' to 'App: 12' 15:53:43.908 Dbg 10739 Oracle: id='12.1' req='834531' SQL: SELECT crf_ +routing.CRF_RT_ACD1_QUEUE,crf_routing.CRF_RT_ACD1_SIZE,crf_ro' +0551 Executed SQL statement 'SELECT', start retrieve records... +0826 MSG_RETRIEVED2 status='DBM_SUCCESS' +0826 MSG_RETRIEVED status='DBM_NOMORE'
<REQIDS_FILE> 834531 834532 834533
foreach $reqidline (<REQIDS_FILE>) { print "reqid - $reqidline\n"; $reqidline =~ s/ */|/g; + # Replace multiple spaces with pipe characters @reqidLineElements = split(/\|/,$reqidline); + # Split line elements into an array $count = 0 ; print "\tactual reqid - $reqidLineElements[0]\n"; foreach $inline (<IN_FILE>) { print "\tinline - $inline\n"; $reqidLineElement = $reqidLineElements[0] ; print "\textracted reqid - $reqidLineElement\n"; # if ($inline=~m/$reqidLineElement/) if (834531 == $reqidLineElement) { # Write the line from the log to output $count = 0 ; next ; print "\tMATCHED - $inline\n"; } if ( ($inline =~ /$reqidLineElements[0]/ ) and +( $inline =~ /SELECT/) ) { # Write current line to output print "\tSELECT - $inline\n"; # Initiate the reading of the next 3 li +nes from the log $counting = "true" ; $count = 0 ; next ; } if ( $counting eq "true" )

Replies are listed 'Best First'.
Re: Matching numeric digits in strings
by davorg (Chancellor) on Jan 10, 2007 at 13:10 UTC

    I think it can probably be a lot simpler than what you've currently got. And the simpler it is, the easier it is to debug and maintain.

    # read required ids into an array my @reqids = <REQIDS_FILE>; chomp @reqids; # build regex containing all required ids my $re = join '|', map { "\Q$_\E" } @reqids; $re = qr/$re/; # Process infile a line at a time looking for matches while (<INFILE>) { if (/$re/) { print "Matched - $_"; if (/SELECT/) { for (1 .. 3) { print scalar <IN_FILE>; } } } }
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Matching numeric digits in strings
by Samy_rio (Vicar) on Jan 10, 2007 at 12:37 UTC

    Hi flemi_p, this will help you if I understood your question correctly.

    use strict; use warnings; open(IN, 'E:\test\infile.txt') || die $!; my @infile = <IN>; close(IN); open(RE, 'E:\test\require.txt') || die $!; my @reqids = <RE>; close(RE); chomp @infile; chomp @reqids; for my $index (0..$#infile){ for my $rindex (0..$#reqids){ if ($infile[$index] =~ m/$reqids[$rindex]/){ if ($infile[$index] =~ m/select/i){ my $count = $index; $count += 3; print "$infile[$_]\n" for ($index..$count); } } } } __END__ Output as: ---------- 15:53:43.908 Dbg 10739 Oracle: id='12.1' req='834531' SQL: SELECT crf_ +routing.CRF_RT_ACD1_QUEUE,crf_routing.CRF_RT_ACD1_SIZE,crf_ro' +0551 Executed SQL statement 'SELECT', start retrieve records... +0826 MSG_RETRIEVED2 status='DBM_SUCCESS' +0826 MSG_RETRIEVED status='DBM_NOMORE'

    Updated Thanks davorg.

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

      map{chomp($_)}@infile; map{chomp($_)}@reqids;

      I think you mean:

      chomp @infile; chomp @reqids;

      Isn't that simpler?

      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

      Hi Velusamy R.,

      Thanks for this. It works fine. However, I need all lines matched with the reqid, which includes the line with the reqid and a select, and the 3 lines in the log file after a line with a select. This constitutes all data for a reqid in the log file and I need them to be output together in the order they are in the log file. Apologies, I didn't explain this very well originally.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://593887]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-16 22:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found