Operating System - HP-UX
1752788 Members
5841 Online
108789 Solutions
New Discussion юеВ

Re: PERL: need a script to manipulate binary files

 
SOLVED
Go to solution
Mel Burslan
Honored Contributor

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...
4 REPLIES 4
H.Merijn Brand (procura
Honored Contributor
Solution

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
Tom Schroll
Frequent Advisor

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.
Tom Schroll
Frequent Advisor

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.
Mel Burslan
Honored Contributor

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...