develooper Front page | perl.perl6.users | Postings from October 2022

Re: How do I print the last three lines in a file?

Thread Previous | Thread Next
Bruce Gray
October 23, 2022 04:11
Re: How do I print the last three lines in a file?
Message ID:

> On Oct 22, 2022, at 10:28 PM, ToddAndMargo via perl6-users <> wrote:
> Hi All,
> Is there a way to print only the last three lines
> in a long file (full on \n's).
> In Windows, I am trying to such the last the  lines is
> > dir /s /A:-D /d /a
> ...
>     Total Files Listed:
>           13671 File(s)  3,265,285,462 bytes
>            3917 Dir(s)  18,406,518,784 bytes free
> And yes, I know how to do it,

It would be generally helpful to tell us the way that you already "know how to do it", so that if our guesswork is insufficiently astute, we don't waste time telling you what you already know.

> I want pretty.
> -T

$ raku -e '.say for 1..1_000_000' > a.1
    # Made a million-line file, for testing

$ time raku -e '.say for lines().tail(3)' a.1
    real	0m2.155s
    user	0m1.727s
    sys	0m0.249s

On Unix or Mac systems (and maybe Windows, UnxUtils or CygWin or GnuWin32 or Microsoft's own "Windows Subsystem for Linux"), faster (and prettier) to pipe to `tail -3`.
$ tail -3 a.1
(and I presume)
C:\> dir /s /A:-D /d /a | tail -3

Hope this helps,
Bruce Gray (Util of PerlMonks)

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About