perlmonknoob has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Arrays and grep problems
by 2teez (Vicar) on Jan 24, 2014 at 12:45 UTC | |
Hi perlmonknoob
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 | [reply] |
|
Re: Arrays and grep problems
by Kenosis (Priest) on Jan 24, 2014 at 19:43 UTC | |
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:
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:
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:
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:
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! | [reply] [d/l] [select] |
|
Re: Arrays and grep problems
by hippo (Archbishop) on Jan 24, 2014 at 13:11 UTC | |
| [reply] | |
|
Re: Arrays and grep problems
by toolic (Bishop) on Jan 24, 2014 at 13:22 UTC | |
Tip #1 from the Basic debugging checklist: use warnings. Tip #2 print. | [reply] |
|
Re: Arrays and grep problems
by Random_Walk (Prior) on Jan 24, 2014 at 15:49 UTC | |
Untested code, but I hope it will give you a hint or two...
Cheers,
Pereant, qui ante nos nostra dixerunt!
| [reply] [d/l] |
|
Re: Arrays and grep problems
by hippo (Archbishop) on Jan 25, 2014 at 10:20 UTC | |
You have changed the code in your original post. Please don't do that as it renders some of the previous answers non-sensical and prevents readers of the whole thread in future from understanding it. Have a read of How do I change/delete my post? particularly the section headed It is uncool to update a node in a way that renders replies confusing or meaningless. Your newer code works as it stands (albeit that one might have written it somewhat differently). If it is printing every line from the log, that will be because every line in the log matches. You can confirm this yourself using the system grep: grep -f /home/user/Desktop/keywords /var/log/syslogIf you have a colourising system grep this will make it very clear where the matches are occurring. | [reply] [d/l] |