Solved

most efficient way to find a string in a file.

Posted on 1998-08-22
6
202 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
Comment Utility
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
Comment Utility
$/="\nbob\n";
if( $ligne=<IN> ){
  print "EXACT match for 'bob' found";
}
0
 
LVL 4

Accepted Solution

by:
mitek earned 50 total points
Comment Utility

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
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.…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

14 Experts available now in Live!

Get 1:1 Help Now