Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: log parser

by alex_fatu (Novice)
on Jun 17, 2013 at 10:06 UTC ( #1039318=note: print w/ replies, xml ) Need Help??


in reply to log parser

can anyone tell me what i do wrong here:

#!/usr/bin/perl use strict; use warnings; print "searching using regexp..."; open(my $in, "<", "DirectX.log") or die "Can't open DirectX.log: $! +"; #while (<$in>) { # assigns each line in turn to $_ # print "Just read in this line: $_"; #} if ($in =~ /eroare/) { print "found eroare\n"; } close $in or die "$in: $!";


Comment on Re: log parser
Download Code
Re^2: log parser
by ultibuzz (Monk) on Jun 17, 2013 at 10:50 UTC

    you do a regex on your filehandler and not it content :)
    try

    #!/usr/bin/perl use strict; use warnings; print "searching using regexp..."; open(my $in, "<", "DirectX.log") or die "Can't open DirectX.log: $! +"; while (<$in>) { # assigns each line in turn to $_ print "Just read in this line: $_"; if ($_ =~ /eroare/) { print "found eroare on line nr: $.\n"; } } close $in or die "$in: $!";

      thank you!!!!
Re^2: log parser
by alex_fatu (Novice) on Jun 17, 2013 at 10:50 UTC
    oke, i found the problem.

    the next code print a msg if word "eroare" is found:

    #!/usr/bin/perl use strict; use warnings; print "searching using regexp...\n"; my $a = "start"; open(my $in, "<", "DirectX.log") or die "Can't open DirectX.log: $! +"; #print $a; while (<$in>) { # assigns each line in turn to $_ #print "Just read in this line: $_"; $a = $a . $_; } if ($a =~ /eroare+/) { print "found eroare\n"; } close $in or die "$in: $!";


    can anyone tell me how can i make it to print every occurance of the match?

      Now you read the whole file into $a (which can be done more efficiently) but what you want is something like

      while (<$in>) { # assigns each line in turn to $_ #print "Just read in this line: $_"; if( /eroare/ ) { # checks $_ print; } }

      check the while loop usage:

      while(my $line=<$in>){ if ($line=~/eroare/i){ print "Found"; } }

      If you tell me, I'll forget.
      If you show me, I'll remember.
      if you involve me, I'll understand.
      --- Author unknown to me

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1039318]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2014-09-19 01:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (129 votes), past polls