Solved

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

Posted on 2007-11-16
9
771 Views
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";
  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;
0
Comment
Question by:matgold
9 Comments
 
LVL 84

Expert Comment

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

Expert Comment

by:MidnightOne
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.

  [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
 

Author Comment

by:matgold
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.
0
 
LVL 39

Expert Comment

by:Adam314
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).
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

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

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

Author Comment

by:matgold
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.
%SYSTEM-F-ABORT, abort
0
 
LVL 39

Accepted Solution

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

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

Author Comment

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

Expert Comment

by:Adam314
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

919 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now