You could also write that as:$bytes =~ s/(?:[^\x0D]|\x0D(?!\x0A))*//;
It's easier to read, but it's very inefficient (see Death to Dot Star! for details).$bytes =~ s/.*?(?=\x0D\x0A)//;
Aside from that, I use read. Read in chunks of an appropriate size and when you find what you need, substitute out what you don't need, write out the rest to a new file and then continue writing the remainder to a file. Of course, don't forget that if you read in say, 20 bytes at a time, the two bytes you specify could be split and you'll need to test to see if 0x0D is on the end of one read and 0x0A is at the beginning of the next.
Hideously untested code:
Cheers,#!/usr/bin/perl -w use strict; my $in_file = 'file1.txt'; my $out_file = 'file2.txt'; open IN, "< $in_file" or die "Can't open $in_file for reading: $!"; open OUT, "> $out_file" or die "Can't open $out_file for writing: $!"; binmode IN; # in case we're on a Windows system binmode OUT; my $buffer; my $flag = 0; my $last_byte = 0; while ( read( IN, $buffer, 1024 ) ) { if ( $last_byte and substr( $buffer, 0, 1 ) == 0x0A ) { $flag = 1; $buffer = substr( $buffer, 1 ); } else { $last_byte = 0; } if ( $buffer =~ /\x0D\x0A/ ) { $flag = 1; $buffer =~ s/(?:[^\x0D]|\x0D(?!\x0A))*//; } $last_byte = 1 if substr( $buffer, -1 ) == 0x0D; last if $flag; } if ( $flag ) { print OUT $buffer or die "Could not write data to $out_file: $!"; while ( read( IN, $buffer, 1024 ) ) { print OUT $buffer or die "Could not write data to $out_file: $ +!"; } } else { warn "Did not find '0x0D 0x0A' in $in_file"; }
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
In reply to (Ovid) Re: Manipulating Binary files
by Ovid
in thread Manipulating Binary files
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |