Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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 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 (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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2018-11-19 15:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My code is most likely broken because:
















    Results (215 votes). Check out past polls.

    Notices?