If you only want the lines upto a unique token, you can set $/ to be that token and the read all the lines in in one go. (See perlman:perlvar and search for $/.

Use split to break these into the individual lines as below.

Note the localization of the setting of $/.

#! perl -sw use strict; my $header; { local $/ = 'END'; open FILE, "<$ARGV[0]" or die "$!\n"; $header = <FILE>; #print tell FILE; close FILE; } my @lines = split /\n/, $header; print $_.$/ for @lines;

That works fine if you only want to read the pre-pended lines. If you want to actually remove them from the file so that you end up with just the binary file again, you could do this with perl but its doubtful if this would be as fast as using tail with a binary offset of the byte after the end token.

The commented out print tell FILE; could be used to get the position to supply the offset for the tail command (eg. tail +nnn file >binonly). This would probably be far more efficient than reading it in and writing it out with Perl as the system utilities have code built in for determining the optimum buffer sizes etc.

There is of course no reason why you shouldn't issue the tail command from within your Perl script:)


Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!

In reply to Re: Reading a Filehandle by line or by stream by BrowserUk
in thread Reading a Filehandle by line or by stream by MadPogo

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.