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

Hi monks -simple question but i have difficulty finding it I have a script that looks for a match in a log file and once it's found it will display it --- now because i 'm looking for 3 parameters at the same time i need to highlight the matches found in red,green and blue so i can see where they are in a log file -so far i was unable to find this -but it's pretty easy to do with grep but not in perl--suggestions?

sub file_in_out { @ARGV == 3 || die "usage: $0 PATTERN LOGNAME DATE \n"; my ( $PAT, $LOG, $DATE ) = @ARGV; open (my $HAN, $LOG) || die "Cannot open '$LOG' because: $!"; #print "ENTRIES FOUND FOR PATTERN = $PAT, LOGFILE=$LOG, DATE=$DATE.... +.............\n"; while ( <$HAN>) { next unless /$DATE/; print "\n", "--------------------------------------------------------------- +--------------------------------------------------------\n", $_ if /$PAT/; }

Replies are listed 'Best First'.
Re: highlighting match found
by keszler (Priest) on Dec 17, 2011 at 20:25 UTC
Re: highlighting match found
by moritz (Cardinal) on Dec 17, 2011 at 20:53 UTC
Re: highlighting match found
by ww (Archbishop) on Dec 17, 2011 at 21:46 UTC
    "a number of tags which makes it hard to read" ? Well, just s/// them away; those you don't need, that is... perhaps using a dispatch table if you need many options .

    But, a more fundamental point: the notion that TJPride's (initial) advice didn't go far enough at was more than just a little hard to infer from your OP. That's is a reason we ask SOPW to post compilable, executable code and sample data.

    Knowing what you're stuffing into $PAT would also be very helpful; it's just a gut feeling at this point, but I'm guessing that if we knew the content of $PAT, we'd have a lot more information upon which to provide some alternate but also useful (maybe more so in your view) advice.

    And since you like the way system grep "pretty easy," you'll do well to read the docs for Perl's grep.

Re: highlighting match found
by roboticus (Chancellor) on Dec 18, 2011 at 02:31 UTC

    auto_w:

    but it's pretty easy to do with grep but not in perl--suggestions?

    ...use grep?

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: highlighting match found
by TJPride (Pilgrim) on Dec 17, 2011 at 21:18 UTC
    Couldn't you just output the matched lines, with line numbers, to a separate file?

      i could but there are still a number of tags which makes it hard to read and pinpoint what i'm looking for

        You could also cheat and make your output file be HTML, which would allow for easy coloring and styling of the text any way you want.

        THIS IS A MATCH!

        <font color="#CC0000"><b>THIS IS A MATCH!</b></font>