Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Searching in binary files

by Corion (Patriarch)
on Dec 14, 2005 at 18:05 UTC ( [id://516709]=note: print w/replies, xml ) Need Help??


in reply to Searching in binary files

Use a sliding window:

my @lines; $/ = \80; # assume a block size of 80 while (<$file>) { push @lines, $_; my $str = join "", @lines; if ($str =~ /searchword/) { my $loc = tell $file - pos $str; print "Found a match starting after $loc.\n"; }; if (@lines > 2) { shift @lines }; };

Instead of looking for a match in just one "line", you look for a match in the "line" and the "line" after it.

Replies are listed 'Best First'.
Re^2: Searching in binary files
by crenz (Priest) on Dec 14, 2005 at 18:25 UTC

    Danke nach Frankfurt!

    There are a few bugs in your code, though. I changed the if to a while loop and fixed a few other problems:

    while ($str =~ /searchword/g) { my $loc = tell($fh) - length($str) + pos($str); print "Found a match starting after $loc.\n"; }

    Update: That doesn't quite work either... it finds too many occurrences...

      while ($str =~ /$pattern/g) { my $loc = tell($fh) - length($str) + pos($str) - length($patte +rn); print "Found a match starting after $loc.\n"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://516709]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-25 21:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found