in reply to search and replace last line

Any ideas on what is the best way to print and replace the last line of a file?

Edit by tye: This was originally a near-duplicate root node

Replies are listed 'Best First'.
Re: Replace Last Line in File
by Abigail-II (Bishop) on Aug 07, 2002 at 14:36 UTC
    perl -pi -we '($_,$@)=($@,$_);$_.="New line\n"if eof' your_file
    Abigail
      Why not <code>perl -pi -we '$_="New line\n"if eof' your_file<code>
Re: Replace Last Line in File
by jmcnamara (Monsignor) on Aug 07, 2002 at 17:39 UTC

    This prints the last line before it has been changed:     perl -i -pe '$last = $_; s/foo/bar/ if eof; END{print $last}' file

    And this changes the last line and then prints it:     perl -i -ne 's/foo/bar/ if eof; print $last = $_; END{print $last}' file

    --
    John.

Re: Replace Last Line in File
by particle (Vicar) on Aug 07, 2002 at 14:34 UTC
    perldoc -q change line file perl 5.008 will tell you to use Tie::File, older perls offer other examples.

    ~Particle *accelerates*