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:
Bruce Gray
Date:
October 23, 2022 05:34
Subject:
Re: How do I print the last three lines in a file?
Message ID:
6480D411-30C4-4D73-8775-21A20A627F45@acm.org


> On Oct 22, 2022, at 11:46 PM, ToddAndMargo via perl6-users <perl6-users@perl.org> wrote:
> 
> 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

You are welcome.

For that particular `dir` use, this is prettier, or at least shorter:
	dir . /s /A:-D /d /a  | raku -e "say lines[*-2].words[2]"

FYI, I just stumbled on a issue with your `s/ .* 'File(s)  '//`code; because you have two spaces after the `(s)`, the program give the wrong answer when the total size of the files exceeds 10 GiB.
The directory tree I tried it on was that big, and so your code reported the size of the subdirectory immediately before the grand total!

-- 
Hope this helps,
Bruce Gray (Util of PerlMonks)



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