getc is very slow and generally shunned. How large is the file? If it can be read into a scalar, you could use the following:
$bytes =~ s/(?:[^\x0D]|\x0D(?!\x0A))*//;
You could also write that as:
$bytes =~ s/.*?(?=\x0D\x0A)//;
It's easier to read, but it's very inefficient (see Death to Dot Star! for details).

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:

#!/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"; }
Cheers,
Ovid

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.