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
From:
ToddAndMargo via perl6-users
Date:
October 23, 2022 04:46
Subject:
Re: How do I print the last three lines in a file?
Message ID:
8e1e8c03-6b5f-664b-55b0-3af635d248ec@zoho.com
On 10/22/22 21:11, Bruce Gray wrote:
> 
> 
>> On Oct 22, 2022, at 10:28 PM, ToddAndMargo via perl6-users <perl6-users@perl.org> 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.
> 
>> but IT AIN'T PRETTY!
>> 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
>      999998
>      999999
>      1000000
>      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
> 

Thank you!

No time of that type in Windows.

This is my non-pretty way.  It takes about two seconds.


 > dir . /s /A:-D /d /a  | raku -e "my Str $x=slurp(); $x~~s/ .* 
'File(s)  '//; $x~~s/ ' bytes' .*//; say $x"

3,275,857,307

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About