Solved

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

Posted on 2007-11-16
9
793 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

749 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