Paul Company wrote:
> 
> I want to replace a string "/usr/local" with another
> string "/tmp/local" in a binary file.

perl -0777pi~ -e's^/usr/local^/tmp/local^' mybinaryfile.out


> This is what I wrote:
> 
> #!/usr/local/bin/perl
> 
> $file = "./mybinaryfile.out";
> $s1 = `strings $file | grep -b /usr/local`; # returned 2027:/usr/local/conf/
> ($byteoffset, $string) = split /:/, $s1;
> $slen = length($string); # returned 16
> open IN, $file or die "Can't open $file for reading: $!\n";
> seek IN, $byteoffset, 0;
> read IN, $rstr, $slen;
> print "$rstr \n";
> 
> What I expected to see was $rstr equal to $string,
> but instead I got a bunch of binary characters.
> Does "grep -b" return the wrong byteoffset?

grep is returning the offset from the output of strings not the offset
in the original file.


> Eventually, after I get past this problem, I'm going
> to modify the string and write it back. Since the length
> of the modified string is the same as the original, it shouldn't
> hurt/break the binary file.


Perl has no problem reading and writing binary data.

#!/usr/local/bin/perl -w
use strict;
my $file = './mybinaryfile.out';
local $/;  # clear the value in $/
open IN, $file or die "Can't open $file for reading: $!";
binmode IN;  # not required on *nix OSs
my $byteoffset = index <IN>, '/usr/local';



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to