most efficient way to find a string in a file.

Posted on 1998-08-22
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/
Question by:steph84

Author Comment

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??!!
LVL 84

Expert Comment

ID: 1204450
if( $ligne=<IN> ){
  print "EXACT match for 'bob' found";

Accepted Solution

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.

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

LVL 84

Expert Comment

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

Expert Comment

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

Expert Comment

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 ...

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

820 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