Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^6: One-liner only checking the first line of the input file

by TJCooper (Beadle)
on Nov 11, 2015 at 15:13 UTC ( [id://1147469]=note: print w/replies, xml ) Need Help??


in reply to Re^5: One-liner only checking the first line of the input file
in thread One-liner only checking the first line of the input file

Same output as before unfortunately.

Replies are listed 'Best First'.
Re^7: One-liner only checking the first line of the input file
by stevieb (Canon) on Nov 11, 2015 at 15:18 UTC

    On the command line, make a backup of the file, then type vi input.txt, then inside of vi, type :set ff=unix, hit ENTER, then type :wq. It would be worth noting whether the file appears as you expect before you run the above commands.

    That'll set line endings to \n (at least it should). Then run your original one-liner to see if that worked.

      Fixed! Brilliant, thank you.

        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);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1147469]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-03-28 08:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found