I wrote a module a while back that deals with issues exactly like this (where the line-endings are different than your platform). File::Edit::Portable.

Here's an example that reads the file, stores the original line endings, modifies them to the local platforms, then writes your output to a temporary file.

It then writes the modified data into a new file, input.txt.new with the original line endings. You can overwrite the original file by omitting the copy parameter in the write() method. read() can return an array of the entire file in list context instead of a file handle, and write() can accept an array reference of file contents in contents as opposed to a file handle. Only use the array technique if your file is small, as the whole thing will be put into memory.

use warnings; use strict; use File::Edit::Portable; my $rw = File::Edit::Portable->new; my $fh = $rw->read('input.txt'); my $wfh = $rw->tempfile; while (<$fh>){ chomp; my @line = split('\s+', $_); print $wfh "$line[2]\t$line[7]\n" if $line[7] > 0; } $rw->write(copy => 'input.txt.new', contents => $wfh);

In reply to Re^9: One-liner only checking the first line of the input file by stevieb
in thread One-liner only checking the first line of the input file by TJCooper

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.