What is the fastest way to do search for keyword in perl?

Posted on 2007-11-16
Medium Priority
Last Modified: 2008-02-01
What is the fastest way to do search for keyword in perl?

I don't knwo whether to used a system command to search for keyword in a files, or to write a perl script. I need to search thru much of text files for a keyword.

#!/PERL/bin/perl -w
$found = "n";
@line = glob "*.txt"
if (!@line) {
  print "no file found\n";
foreach $ln (@line) {
$lineNo = 0;
@lines = <FILE>;

foreach $line (@lines) {

    # increment line counter

  if (($line =~ m/(Speedy)/io) || ($line =~ m/(TEST)/io)){
     print "$line at line $lineNo of job_sample.txt\n";
     $found = "y";
if ($found =~ "n") {
  print "no keyword found\n";
Question by:matgold
LVL 85

Expert Comment

ID: 20300333
on unix
system("grep -in Speedy *.txt")
LVL 26

Expert Comment

ID: 20307816
In windows XP, FIND works.
C:\Documents and Settings\Midnightone>find /?
Searches for a text string in a file or files.
FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string" [[drive:][path]filename[ ...]]
  /V         Displays all lines NOT containing the specified string.
  /C         Displays only the count of lines containing the string.
  /N         Displays line numbers with the displayed lines.
  /I         Ignores the case of characters when searching for the string.
  /OFF[LINE] Do not skip files with offline attribute set.
  "string"   Specifies the text string to find.
             Specifies a file or files to search.
If a path is not specified, FIND searches the text typed at the prompt
or piped from another command.

Open in new window


Author Comment

ID: 20313867
FYI, this is a open/vms

command:  search *.txt "keyword"
it is taking a long time, due to lots of file and size.
I just though there is a better way.
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 39

Expert Comment

ID: 20314147
Having perl search the actual files would probably not be any faster, as it would have just as many files/bytes to search through.

You might be able to create an index (which is slow, but you do only when a file changes), then you search that index (which is fast).

Expert Comment

ID: 20319281
egrep "Speedy|TEST" *.txt

this is the fastest command, no matter file size.
64bit machines will enhance performance on large DB.

Author Comment

ID: 20407382
I try to compare to "perl -ne 'print if (/REMIT/)'  DEMON.TXT"
but it is not working.

syntax error at -e line 1, at EOF
Execution of -e aborted due to compilation errors.
LVL 39

Accepted Solution

Adam314 earned 200 total points
ID: 20407622
Try using double-quotes instead of single quotes:

perl -ne "print if /REMIT/" DEMON.TXT

Author Comment

ID: 20412079
thank you sir, the syntax work.
is not any faster than a regular search command.
LVL 39

Expert Comment

ID: 20412907
I wouldn't expect it to be.  If you are searching frequently, you might want to consider creating an index.  This step is slow, but the searching is very fast.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question