Re: PERL: need a script to manipulate binary files (83 Views)
Reply
Honored Contributor
Mel Burslan
Posts: 3,212
Registered: ‎08-26-1998
Message 1 of 5 (83 Views)
Accepted Solution

PERL: need a script to manipulate binary files

I am in search of a script or set of commands in PERL (or any other scripting language for that matter) which will open a binary file, read certain number of bytes, skipping over an offset # of bytes from the beginning, and write these bytes into another file.

without any respect to syntax, this is what I am trying to accomplish:

offset=variable1
length=variable2
open_for_read (INHANDLE, inputfile)
open_for_write (OUTHANDLE, outputfile)
rec=read (INHANDLE, offset, length)
write (OUTFILE, rec)

my PERL programming skills is one step far from none-existent. So far any kind of perl for beginners type document I could find online and off, talks about opening and reading from text files. I know this is what PERL is designed for but wondering if there is any way to read a binary file byte-by-byte. If yes, how can I do this ?

All in all what I am trying to do is to chop up some binary files into several smaller chunks, and copying them into my windows machine and using one of those splitters does not work for me. The commercial products are designed to chop up the stuff into smaller fixed size chunks and it is not what I am looking for.

As always thanks in advance...
________________________________
UNIX because I majored in cryptology...
Please use plain text.
Honored Contributor
H.Merijn Brand (procura
Posts: 6,185
Registered: ‎10-13-1997
Message 2 of 5 (83 Views)

Re: PERL: need a script to manipulate binary files

my $offset = 45; # offset hard coded
my $length = $ENV{variable2}; # length in environment variable
my $file = $ARGV[0]; # filename passed as first command line argument

open my $filehandle, "< $file" or die "$file: $!";
open my $ofile, "+< file.out" or die "file.out: $!";

my $buffer;
seek $filehandle, $offset, 0;
read $filehandle, $buffer, $length;

seek $ofile, 1245, 0; # write at position 1245
syswrite $ofile, $buffer;

close $filehandle;
close $ofile;

Don't forget to count the newline characters in your offsets

Proof of concept:

lt09:/tmp 115 > cat xx.txt
0123456789
1234567890
lt09:/tmp 116 > cat yy.txt
0123456789
1234567890
lt09:/tmp 117 > perl -we'open$in,"lt09:/tmp 118 > cat xx.txt
0123456789
1234567890
lt09:/tmp 119 > cat yy.txt
0123456789
5674567890
lt09:/tmp 120 >

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Please use plain text.
Frequent Advisor
Tom Schroll
Posts: 38
Registered: ‎11-07-2003
Message 3 of 5 (83 Views)

Re: PERL: need a script to manipulate binary files

If you want to do this with just a shell command instead of perl:

dd bs=1 skip=bytes_to_skip if=file1 > file2

Whereas bytes_to_skip is n blocks to skip, and since we are setting the block size to 1 byte, it will skip n bytes.

dd is quite efficient for these type of tasks and is available on just about every Unix system out of the box.


Here is an example:

# dd bs=1 skip=3 if=usflag.gif > x.gif
9347+0 records in
9347+0 records out
# ls -al usflag.gif x.gif
-rwxr--r-- 1 ths staff 9350 Sep 17 2001 usflag.gif
-rw-rw-r-- 1 ths staff 9347 Jul 15 16:48 x.gif

You'll see that the binary file is 3 bytes smaller, which are removed from the beginning according to the skip directive to dd.

-- Tom
If it ain't broke, it needs optimized.
Please use plain text.
Frequent Advisor
Tom Schroll
Posts: 38
Registered: ‎11-07-2003
Message 4 of 5 (83 Views)

Re: PERL: need a script to manipulate binary files

Ooops, in my dd example, I also forgot to include the count=n option, which will also control how many bytes are read in, so you can control the size. Using a combination of skip and count, you should be able to break up binary files into pieces relatively efficiently.

Check the dd man page for more info.

Hope this helps...

-- Tom
If it ain't broke, it needs optimized.
Please use plain text.
Honored Contributor
Mel Burslan
Posts: 3,212
Registered: ‎08-26-1998
Message 5 of 5 (83 Views)

Re: PERL: need a script to manipulate binary files

Both solutions worked great.

Thanks for pointing out the dd option. Never crossed my mind prior to reading it here.

________________________________
UNIX because I majored in cryptology...
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation