We help IT Professionals succeed at work.

most efficient way to find a string in a file.

steph84
steph84 asked
on
305 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/
???
Comment
Watch Question

Author

Commented:
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.
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
$/="\nbob\n";
if( $ligne=<IN> ){
  print "EXACT match for 'bob' found";
}
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
steph84 said exact match, not word boundary match,
but word boundary match does seem more likely to be what was meant.

Commented:
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).  

Commented:
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 ...
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.