Solved

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

Posted on 2007-11-16
9
786 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Removing file extension within a file. 4 98
crawling gofundme 4 132
collecting information 2 181
Put query string from URL request -  PERL HTTP Daemon 2 85
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…
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 …
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…

829 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