Link to home
Start Free TrialLog in
Avatar of steph84
steph84

asked on

most efficient way to find a string in a file.

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/
???
Avatar of steph84
steph84

ASKER

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.
Avatar of ozo
$/="\nbob\n";
if( $ligne=<IN> ){
  print "EXACT match for 'bob' found";
}
ASKER CERTIFIED SOLUTION
Avatar of mitek
mitek

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
steph84 said exact match, not word boundary match,
but word boundary match does seem more likely to be what was meant.
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).  
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 ...