naturalsciences has asked for the wisdom of the Perl Monks concerning the following question:
Reaction is that it will only take the last line and print it in front of the file and then generate a lots of empty lines. Can anybody help me with this code or suggest a better one. JP Updated. Thanks for your replys. I found a module File::ReadBackwards that soothed all my problems in a sec :D Though im still curious whats the problem with my original code. And no it is not somekind of a programming homework - I have to work with several bioinformatics programs for my thesis, and being academic software they are usually not at all meant for compability. So i have to reformat outputfiles from some of them to use as input files for others. Sometimes the formatting differences are small and easy, sometimes differences are large but solutions are easy and this time the difference was small ( reverse the file) but solution just dindt came to my mind :D Also files tend to be in several Gb-s so any solutions involving reading those files in memory are out. ( makes my life harder)#!/usr/bin/perl -w use strict; use warnings; my $file = 'hugefiletobereversed'; open FH, 'hugefiletobereversed'; while (our $newline = <FH>) { { local @ARGV = ($file); local $^I = '.bac'; while(<>){ if ($. == 1) { print "$newline\n"; } else { print; } } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: reversing files line by line
by Corion (Patriarch) on Feb 14, 2010 at 15:08 UTC | |
by naturalsciences (Beadle) on Feb 16, 2010 at 09:47 UTC | |
|
Re: reversing files line by line
by bart (Canon) on Feb 14, 2010 at 16:25 UTC | |
|
Re: reversing files line by line
by afoken (Chancellor) on Feb 14, 2010 at 19:45 UTC | |
|
Re: reversing files line by line
by repellent (Priest) on Feb 14, 2010 at 19:49 UTC | |
|
Re: reversing files line by line
by Marshall (Canon) on Feb 15, 2010 at 07:52 UTC |