Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Print all lines in a file except the last two

by Anonymous Monk
on May 20, 2004 at 13:54 UTC ( [id://354947]=perlquestion: print w/replies, xml ) Need Help??

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

I'm pretty new to Perl and programming in general. I have a shell script that I'm currently supposed to modify. It takes an output file of data and converts in to a sort of spreadsheet format. The only thing is that it puts two lines of additional calculations at the end of each data set. Any ideas on how to remove those last two lines in each of the data sets?

Edit by castaway, changed title from "Help".

Replies are listed 'Best First'.
Re: Print all lines in a file except the last two
by diotalevi (Canon) on May 20, 2004 at 14:06 UTC

    Yes - a small queue would do this. Since you're so new, you'll do yourself a world of good by picking up a copy of the inexpensive book Learning Perl (also known as the Llama book) from O'Reilly.

    while (<>) { push @queue, $_; print shift @queue if @queue > 2; }
Re: Print all lines in a file except the last two
by neniro (Priest) on May 20, 2004 at 15:10 UTC
    You can use Tie::File:
    #!/usr/bin/perl use strict; use warnings; use Tie::File; my $file = "file.txt"; my @lines; tie @lines, 'Tie::File', $file or die $!; print $_,"\n" for (@lines[0 .. $#lines-2]); untie @lines;
Re: Print all lines in a file except the last two
by mifflin (Curate) on May 20, 2004 at 14:36 UTC
    This works for me...
    $lines = `/usr/bin/wc -l afile.txt`; open(F, '<afile.txt') || die 'cannot open afile.txt'; $cntr = 0; while (<F>) { $cntr++; print; last if $cntr == $lines - 2; }
    on my unix box.

      There is a magic variable that keeps track of the current line number from the last-read filehandle. This would make your while loop a bit simpler:

      $. < $lines - 2 and print while <F>
Re: Print all lines in a file except the last two
by mifflin (Curate) on May 20, 2004 at 14:49 UTC
    another idea...
    If you don't want the last two lines you could truncate them
    open(F, '+<afile.txt') || die 'cannot open afile.txt'; while (<F>) { push(@addrs, tell(F)) unless eof(F); } truncate(F, $addrs[-2]) || die 'cannot truncate afile.txt';
    This will remove the last two lines in a file.
    I got the idea for this in the Perl Cookbook. Recipe 8.10
    If you don't have that book I recommend it.
Re: Print all lines in a file except the last two
by Ryszard (Priest) on May 20, 2004 at 14:56 UTC
    Assuming a CR after each line:
    1 2 3 4 1234567890123456789012345678901234567890123456789 perl -e 'open(F,'f');@f=<F>;pop@f;pop@f;print"$_"foreach@f'

      You can remove the pops and change the print to print@f[0..$#f-2] for a savings of 13 strokes.

      ----
      send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

      Why a counter and imply it's short when you have a foreach, and a print"$_" ?

      print for@f[0..-2] is shorter.

      Edit: Wow, I really do live in a little fluffy bunny world where everyone's happy, there's no war, pizza isn't fattening, Darth Vader was never an annoying child, the Six million Dollar Man really was a good tv show, new shoes are comfy, my girlfriend's bum never looks big in this, and code like this works.

      Kind of surprised I didn't lose all the xp I'd ever gotten on this one. hardburn posted the correct code below.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-03-28 22:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found