in reply to Deleting last line of file


This is a simple one-liner:     perl -i -ne 'print unless eof' file

Update: see also Delete the last line of a file.

--
John.

Replies are listed 'Best First'.
Re: Re: Deleting last line of file
by Anonymous Monk on Aug 07, 2002 at 18:24 UTC
    I'm trying to run the shell command in my script, but for some reason, it's not working....can anyone pinpoint the problem? thanks
    my @args = qw/ perl -i -ne 'print unless eof' filename /; system(@args) == 0 or die "error with deleting last line";
      I don't think you understand what qw does. Try running this and I think you will figure it out.
      my @args = qw/ perl -i -ne 'print unless eof' filename /; print join "|", @args;

      --

      flounder


      That was just a one-liner.

      If you need to do something more sophisticated as part of a program you should try something like this.

      #!/usr/bin/perl -w use strict; # Localised in-place edit { local $^I = ''; # or use '.bak' to save a back-up local @ARGV = 'somefile'; while (<>) { print unless eof; } } __END__

      --
      John.