Solved

most efficient way to find a string in a file.

Posted on 1998-08-22
6
215 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

22 Experts available now in Live!

Get 1:1 Help Now