For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > June 2005 > perl one liner to display the third line from the end of a file









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author perl one liner to display the third line from the end of a file
Oxnard

2005-06-07, 8:57 pm

perl v5.8.0

Assuming the file is a random length text file.
I know I can do this in a Perl script, however I would like to add the one
liner to an shell script.
Anyone have idea ideas on how to do this?



Jim Gibson

2005-06-07, 8:57 pm

In article <caadnSguucGMaDjfRVn-pA@comcast.com>, Oxnard
<shankeypNO_SPAM@comcast.net> wrote:

> perl v5.8.0
>
> Assuming the file is a random length text file.
> I know I can do this in a Perl script, however I would like to add the one
> liner to an shell script.
> Anyone have idea ideas on how to do this?


You know you can do what? Oh, wait. The question is in the Subject
line. You are more likely to get good answers if you put your question
in the Body of your message and otherwise help people to help you.

Anyway, while it is not too efficient for big files, this works:

perl -e '@f=<>;print $f[-3]' file


----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
Brian Wakem

2005-06-07, 8:57 pm

Oxnard wrote:

> perl v5.8.0
>
> Assuming the file is a random length text file.
> I know I can do this in a Perl script, however I would like to add the one
> liner to an shell script.
> Anyone have idea ideas on how to do this?



If you are on a proper OS then tail and head would be your best bet I would
think.


system("tail -3 filename | head -1");



What do you want to happen if there are less than 3 lines in the file?



--
Brian Wakem

Charles DeRykus

2005-06-08, 3:59 am

In article <caadnSguucGMaDjfRVn-pA@comcast.com>,
Oxnard <shankeypNO_SPAM@comcast.net> wrote:
>perl v5.8.0
>
>Assuming the file is a random length text file.
>I know I can do this in a Perl script, however I would like to add the one
>liner to an shell script.
>Anyone have idea ideas on how to do this?
>


yes, but the one-liner society may shun you...

perl -MFile::ReadBackwards -e '$b=File::ReadBackwards->new("your_file");
$l=$b->realine for 1..3;print $l'

--
Charles DeRykus
Anno Siegel

2005-06-08, 8:58 am

Oxnard <shankeypNO_SPAM@comcast.net> wrote in comp.lang.perl.misc:
> perl v5.8.0
>
> Assuming the file is a random length text file.
> I know I can do this in a Perl script, however I would like to add the one
> liner to an shell script.


perl -ne 'print if $. eq 3' file

If the file is big:

perl -ne '$. eq 3 and print and last' file

Anno
Bernard El-Hagin

2005-06-08, 3:59 pm

anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote:

> Oxnard <shankeypNO_SPAM@comcast.net> wrote in comp.lang.perl.misc:
>
> perl -ne 'print if $. eq 3' file



Subject: Re: perl one liner to display the third line from the end of a
^^^^^^^^^^^^
file


--
Cheers,
Bernard
Anno Siegel

2005-06-08, 3:59 pm

Bernard El-Hagin <bernard.el-haginDODGE_THIS@lido-tech.net> wrote in comp.lang.perl.misc:
> anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote:
>
>
>
> Subject: Re: perl one liner to display the third line from the end of a
> file


Right. Goes to show it's a bad idea to put the question only in the
subject.

perl -ne 'shift @x if @x > 2; push @x, $_; END{ print @x[ 0]}' file

Anno
Damian James

2005-06-08, 3:59 pm

On Tue, 7 Jun 2005 14:27:10 -0500, Oxnard said:
> Assuming the file is a random length text file.
> I know I can do this in a Perl script, however I would like to add the one
> liner to an shell script.
> Anyone have idea ideas on how to do this?


perl -ne 'BEGIN{$#x=2}push@x,$_;shift@x}{print $x[0]' file

is pleasantly obscure.

Though as another poster suggested, tail -3 | head -1 is the
way to go if obscurity is not on the wish list.

--damian
--
@:=grep!(m!$/|#!..$|),split//,<DATA>;@;=0..$#:;while($:=@;){$;=rand
$:-- ,@;[$;,$:]=@;[$:,$;]while$:;push@|,shift
@;if$;[0]==@|;select$,,
$,,$,,1/80;print qq x\bxx((@;+@|)*$|++),@:[@|,@;],!@;&&$/} __END__
Just another Perl Hacker,
Glenn Jackman

2005-06-08, 3:59 pm

At 2005-06-07 03:27PM, Oxnard <shankeypNO_SPAM@comcast.net> wrote:
> perl v5.8.0
>
> Assuming the file is a random length text file.
> I know I can do this in a Perl script, however I would like to add the one
> liner to an shell script.
> Anyone have idea ideas on how to do this?


Not a high golf score, but you don't have to read every line in the
file. It assumes that the final 3 lines can be contained in the last
1024 bytes of the file though.

perl -e '
open $fid, "<", shift;
$buf = 1024;
s $fid, -$buf, 2;
$bytes = read $fid, $data, $buf;
@lines = split /\n/, $data, -1; # preserve trailing blank lines
pop @lines; # but ignore the trailing newline that ends the file
print $lines[-3], "\n"
' file


--
Glenn Jackman
NCF Symin
glennj@ncf.ca
Brian McCauley

2005-06-08, 3:59 pm



Damian James wrote:

> On Tue, 7 Jun 2005 14:27:10 -0500, Oxnard said:
>
>
> perl -ne 'BEGIN{$#x=2}push@x,$_;shift@x}{print $x[0]' file
>
> is pleasantly obscure.
>
> Though as another poster suggested, tail -3 | head -1 is the
> way to go if obscurity is not on the wish list.


If obscurity is not on the list but Perl is, then Damian's solution is
more clearly written as:

perl -ne 'BEGIN{ $#x=2 } push @x,$_; shift @x; END{ print $x[0] }' file

Damian James

2005-06-08, 8:57 pm

On Wed, 08 Jun 2005 18:48:28 +0100, Brian McCauley said:
> Damian James wrote:
> ...
> If obscurity is not on the list but Perl is, then Damian's solution is
> more clearly written as:
>
> perl -ne 'BEGIN{ $#x=2 } push @x,$_; shift @x; END{ print $x[0] }' file


*giggles*

Actually, I like Anno's better: principal of least surprise for a file with
less than 3 lines seems to me to be to print the first one, whereas mine
tries to print undef. Oh well.

--damian
John W. Krahn

2005-06-09, 3:57 am

Damian James wrote:
> On Tue, 7 Jun 2005 14:27:10 -0500, Oxnard said:
>
>
>
> perl -ne 'BEGIN{$#x=2}push@x,$_;shift@x}{print $x[0]' file
>
> is pleasantly obscure.


How about :

perl - ne'BEGIN{$#x=1}($\,@x)=(splice(@x),$_)}{
print' file


John
--
use Perl;
program
fulfillment
Bart Lateur

2005-06-09, 8:57 am

Oxnard wrote:

>Assuming the file is a random length text file.


Try File::ReadBackwards.

<http://search.cpan.org/search?modul...::ReadBackwards>

--
Bart.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com