Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   perl one liner to display the third line from the end of a file (http://www.velocityreviews.com/forums/t892742-perl-one-liner-to-display-the-third-line-from-the-end-of-a-file.html)

Oxnard 06-07-2005 07:27 PM

perl one liner to display the third line from the end of a file
 
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?




Brian Wakem 06-07-2005 08:27 PM

Re: perl one liner to display the third line from the end of a file
 
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 06-08-2005 01:40 AM

Re: perl one liner to display the third line from the end of a file
 
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 06-08-2005 11:10 AM

Re: perl one liner to display the third line from the end of a file
 
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

Anno Siegel 06-08-2005 12:01 PM

Re: perl one liner to display the third line from the end of a file
 
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:
>
> > 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

>
>
> 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 06-08-2005 01:26 PM

Re: perl one liner to display the third line from the end of a file
 
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 06-08-2005 05:01 PM

Re: perl one liner to display the third line from the end of a file
 
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;
seek $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 Sysadmin
glennj@ncf.ca

Brian McCauley 06-08-2005 05:48 PM

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


Damian James wrote:

> 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.

>
> 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 06-08-2005 11:00 PM

Re: perl one liner to display the third line from the end of a file
 
On Wed, 08 Jun 2005 18:48:28 +0100, Brian McCauley said:
> Damian James wrote:
>> perl -ne 'BEGIN{$#x=2}push@x,$_;shift@x}{print $x[0]' file
>>
>> is pleasantly obscure.

> ...
> 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 06-09-2005 02:45 AM

Re: perl one liner to display the third line from the end of a file
 
Damian James wrote:
> 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.


How about:

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


John
--
use Perl;
program
fulfillment


All times are GMT. The time now is 03:58 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.