Solved

most efficient way to find a string in a file.

Posted on 1998-08-22
6
224 Views
Last Modified: 2010-03-04
What's the most efficient way to find a string in a file?
reading the file line by line and make a match?
while $ligne=<IN>
if $ligne =~ /pattern/
???
0
Comment
Question by:steph84
6 Comments
 

Author Comment

by:steph84
ID: 1204449
Oh yes, and something I forgot, I want to make an EXACT match, I don't want that bob searched in bobby return true.
How to make an exact match??!!
Thanks.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1204450
$/="\nbob\n";
if( $ligne=<IN> ){
  print "EXACT match for 'bob' found";
}
0
 
LVL 4

Accepted Solution

by:
mitek earned 50 total points
ID: 1204451

undef $/;
$data = <IN>;
unless ($data =~ /\bbob\b/){
  print "EXACT match for 'bob' found";
}
undef $data

P.S. $/="\nbob\n" would find bob only if this word is on a separate line all by itself. /\bbob\b/ will look for any word boundary.

0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 84

Expert Comment

by:ozo
ID: 1204452
steph84 said exact match, not word boundary match,
but word boundary match does seem more likely to be what was meant.
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1204453
Oh, come on... at least a warning here....

Although it's definitely faster (well, maybe not definitely--marginally may sometimes be the appropriate word, depends on your buffer size, disk sector size, etc) to read in that entire file, that entire file may be of such a size that you start paging huge quantities everytime you try and move. (I'll grant that I'm only talking about files which are, say, 1/4 the size of your physical memory).  
0
 
LVL 4

Expert Comment

by:mitek
ID: 1204454
that's right. if you even suspect that you may have a file of that size, the algorithm would be different and more complicated. then, it would be necessary to read in blocks of let's say, 4M in memory one by one, do lookup, load in another block, etc. probably, an overkill if one doesn't deal with files > 4M ...
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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