Dear Monks,

I need to process BIG files which for disk-size purposes I have to keep zipped. So I'm piping my data into my perl script using gunzip... For my script to work I need to sometimes peep one line ahead but then reset the file handle position to current position - 1 line. When using normal files my script works but somehow the seek function fails when I open a pipe instead of a file... I have made a test script which replicates the problem..

#!/usr/bin/perl use warnings; use strict; # Test linereading open (TEST, 'test.txt') or die "Unable to open test.txt file: $!\n"; #open (TEST, "gunzip -c test.txt.gz |") or die "Unable to open pipe: $ +!\n"; while (<TEST>) { print "$_"; seek(TEST, -length($_), 1); # place the same line back onto the fi +le handle }
The test.txt looks like this:
This is the first line
This is the second line
This is the third line

If I open the 'file' test.txt as normal this program should give me an eternal loop printing only "This is the first line";

If I now instead of open 'test.txt' open a pipe using gunzip to give me the data in test.txt.gz the script will print each line and exit after the last. Which means that the 'go back one line didn't work.

Can anybody tell me how to deal with this and go back 1 line in a file handle for a pipe? Thank you all in advance!

Regards,
John van Dam


In reply to reset filehandle position on a buffered pipe in by johnvandam

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.