Solved

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

Posted on 2007-11-16
9
758 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 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

19 Experts available now in Live!

Get 1:1 Help Now