Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

print last line

by httpd (Novice)
on Apr 08, 2013 at 09:43 UTC ( #1027453=perlquestion: print w/replies, xml ) Need Help??
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
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 (Bishop) 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 (Chancellor) 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!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1027453]
Approved by Corion
NodeReaper slips on a banana skin

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (1)
As of 2018-07-23 02:18 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (459 votes). Check out past polls.