You've been given excellent tips for improving your script. In an earlier response to you, Laurent_R suggested two methods for accomplishing your task. The following addresses one of those, as I think it's a good option.

When you're looking for keywords in each line, you're effectively joining them with OR. For example: print the current file's line if it contains 'Hello' OR 'world' OR 'today'. In a regex, the OR function is accomplished using alternation:

/(?:Hello|world|today)/

The (?:) notation forms a non-capturing group. It's not strictly necessary here, but is used to just cluster the set of disjuncts.

There are a few issues with the above regex which need to be addressed. One is that, as it is, it's case sensitive. That is, 'today' would match but 'Today' wouldn't. To create a case-insensitive match, use the i modifier:

/(?:Hello|world|today)/i

The next issue is that the above regex would match 'worldly'--and you may not want in-string matching. To prevent in-string matching, you need to match words enclosed by word boundaries:

/(?:\bHello\b|\bworld\b|\btoday\b)/i

The last item to consider comes when you may want to match a phrase like 'Mr. Smith'. The problem is that the period in the string is a regex meta-character used to match one character. This period, and other meta-characters, must be escaped for a literal match: 'Mr\. Smith'.

Give the above considerations, the script below implements them:

use strict; use warnings; use autodie; my @arr; my $logz = '/var/log/syslog'; my $file = '/home/user/Desktop/keywords'; open my $keysFH, '<', $file; while (<$keysFH>) { chomp; push @arr, "\\b\Q$_\E\\b"; } close $keysFH; my $words = '(?:' . ( join '|', @arr ) . ')'; my $regex = qr/$words/i; open my $logFH, '<', $logz; while (<$logFH>) { print if /$regex/; } close $logFH;

The "\\b\Q$_\E\\b" notation first quotes all meta-characters in the word (\Q...\E) and the \\b means word boundary. There are two "\", because the first escapes the second, leaving the literal "\b" in the string.

Print both $words and $regex to see what's constructed.

Usage: perl script.pl [>outFile]

The last, optional parameter directs output to a file.

Hope this helps!


In reply to Re: Arrays and grep problems by Kenosis
in thread lolhackedlol by perlmonknoob

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.