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

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";
  exit;
}
#
foreach $ln (@line) {
$lineNo = 0;
open(FILE,"$ln");
@lines = <FILE>;
close(FILE);

foreach $line (@lines) {

    # increment line counter
    $lineNo++;

  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";
}
exit;
matgoldAsked:
Who is Participating?
 
Adam314Commented:
Try using double-quotes instead of single quotes:

perl -ne "print if /REMIT/" DEMON.TXT
0
 
ozoCommented:
on unix
system("grep -in Speedy *.txt")
0
 
MidnightOneCommented:
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.
  [drive:][path]filename
             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

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
matgoldAuthor Commented:
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.
0
 
Adam314Commented:
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).
0
 
TalmashCommented:
egrep "Speedy|TEST" *.txt

this is the fastest command, no matter file size.
64bit machines will enhance performance on large DB.
0
 
matgoldAuthor Commented:
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.
%SYSTEM-F-ABORT, abort
0
 
matgoldAuthor Commented:
thank you sir, the syntax work.
is not any faster than a regular search command.
0
 
Adam314Commented:
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.
0
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.

All Courses

From novice to tech pro — start learning today.