Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

removing lines from a file

by Anonymous Monk
on Feb 18, 2005 at 10:14 UTC ( [id://432248]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks. I have a file cut_out
cut_out 8 2
i need to copy the first 8 lines of ORIGINAL to a new file 1.out.test. then print the next 2 lines to 2.out.test
ORIGINAL 1 2 3 4 TO 10
open (true, "<ORIGINAL"); open (edit, "<cut_out2"); open (rest, ">REST"); while (<edit>) { chomp; $j = $_; print $j; open (out, ">$.out.test"); while (<true>){ if (1..$.==$j) {print out} else {print rest}; print "\nCUT"; rename (REST, COMPARE); } } ~
works for the first copy but not the second? thanks

Replies are listed 'Best First'.
Re: removing lines from a file
by Random_Walk (Prior) on Feb 18, 2005 at 10:40 UTC

    Please use strict and warnings. Please do not use a filehandle called true, then have a while (<true>) that will go false. That hurt my head ;)

    It is good practice to test the result of attempts to open a file, and polite to close them when you have finished reading from them.

    perltidy is your friend

    The three argument form of open is safer

    update

    Added a check that there is something remaining in the original file while editing
    #!/usr/bin/perl use strict; use warnings; # true was a confusing name open ORIG, "<", "ORIGINAL" or die "probs: ORIGINAL : $!\n"; open EDIT, "<", "cut_out" or die "probs: cut_out2 : $!\n"; open REST, ">", "REST" or die "probs: REST : $!\n"; while (<EDIT>) { chomp; my $j = $_; print "grabbing $j lines to $.out.test\n"; open OUT, ">", "$.out.test" or die "can not open $.out.test : $!\n +"; while ($j--){ if (my $line = <ORIG>) { print OUT $line; } else { die "Ran out of input file before editing complete !\n"; } } close OUT; } close EDIT; while (<ORIG>) { print REST; } close ORIG; close REST; # rename ("REST", "COMPARE");

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
      thanks for that, works exactly the way i wanted cheers
Re: removing lines from a file
by gopalr (Priest) on Feb 18, 2005 at 10:31 UTC

    Hi,

    Your whole coding is not good.

    Try the below:

    open (true, "< DATA.txt"); open (first, "> 1.out.text"); open (rest, "> 2.out.text"); $. <= 8 ? print first $_ : print rest $_ while(<true>);

    DATA.txt

    1 2 3 4 5 6 7 8 9 10

    Update: true is included

      But it doen't take into account the data in the cut_out file.

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      the output files are empty? when u use the above

        Here it's working fine. I don't know what u do.

        Just copy the coding and run it.

        Still if u face the problem again, let me know.

        oooppps <DATA> shoule be <true> so how can i next remove the following two lines, bearing in mind that i may have 30 or so continous deletions to perform?
Re: removing lines from a file
by Anonymous Monk on Feb 18, 2005 at 10:40 UTC
    when i try to print out COMPARE after renaming its denied?
Re: removing lines from a file
by Miguel (Friar) on Feb 18, 2005 at 17:35 UTC
    Another alternative:

    Updated: to include the cut_out.txt file.

    #!/usr/bin/perl -w use strict; use IO::All; my @cut_out = io("cut_out.txt")->slurp; my @lines = io('original.txt')->slurp; my $total_lines = 0; $total_lines += $_ foreach @cut_out; for (my $i = 0; $i <= ($total_lines - 1); $i++ ) { $i <= $cut_out[0] - 1 ? io("1.out.test.txt")->append($lines[$i]) : io("2.out.test.txt")->append($lines[$i]) }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-26 08:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found