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

Hi monks, I have a very simple question, for some reasons I cannot find regex for printing just a last line from file, basically the same as sed did with just 1 symbol:
sed -n '$p'
I just found how to print lines from specific number till the end:
# perl -ne 'print if (9../^\n/)' urls.txt http://www.0534343434tutorials.com http://mer3nharing.com
But what regex should I use for printing just a last line? as simple as in sed.

Replies are listed 'Best First'.
Re: print last line
by choroba (Cardinal) on Apr 08, 2013 at 10:01 UTC
    You can use the eof function:
    perl -ne 'print if eof'
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: print last line
by Loops (Curate) on Apr 08, 2013 at 09:57 UTC
    There is probably a smarter way than this:
    perl -ne '$l=$_ ; END { print $l; }'
Re: print last line
by kcott (Archbishop) on Apr 08, 2013 at 10:54 UTC

    G'day httpd,

    It's unlikely that you really want a regex; however, if you do, here's one way to do it:

    $ cat pm_1027453.dat line1 line2 line3
    $ perl -Mstrict -Mwarnings -E ' say do { local $/; <> } =~ /(.*)\Z/m; ' pm_1027453.dat line3

    You've already been given non-regex solutions. Here's another using Tie::File:

    $ perl -Mstrict -Mwarnings -E ' use Tie::File; tie my @lines, q{Tie::File}, q{./pm_1027453.dat} or die $!; say $lines[$#lines]; untie @lines; ' line3

    You may want to look at Benchmark to see which solution best suits your application.

    -- Ken

Re: print last line
by httpd (Novice) on Apr 08, 2013 at 11:12 UTC
    Thank you guys, solved!