Home > Archive > PERL Beginners > July 2005 > perl newbie question
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 newbie question
|
|
| Kaushik M 2005-07-25, 9:24 am |
|
hi ,
I am a perl newbie.
Can someone suggest a perl command line snippet that will print the last n
lines of a file.
thanks in advance.
regards,
Kaushik
Notice: The information contained in this e-mail message and/or attachments to it may contain confidential or privileged information. If you are not the intended recipient, any dissemination, use, review, distribution, printing or copying of the informa
tion contained in this e-mail message and/or attachments to it are strictly prohibited. If you have received this communication in error, please notify us by reply e-mail or telephone and immediately and permanently delete the message and any attachment
s. Thank you
| |
| Edward WIJAYA 2005-07-25, 5:30 pm |
| On Mon, 25 Jul 2005 21:39:50 +0800, <kaushik.m@tcs.com> wrote:
> Can someone suggest a perl command line snippet that will print the last
> n
> lines of a file.
If you are under unix/linux
just use "tail -n" command.
However if you really want to go via Perl command line:
$perl -e '
open FH, "<file" or die $!;
@lines = <FH>;
close FH or die $!;
foreach $i (($#lines - $n) .. $#lines) { print "$lines[$i]\n";}
'
--
Regards,
Edward WIJAYA
SINGAPORE
| |
| Keith Worthington 2005-07-25, 5:30 pm |
| > hi ,
> I am a perl newbie.
> Can someone suggest a perl command line snippet that will print the last n
> lines of a file.
>
> thanks in advance.
> regards,
> Kaushik
Kaushik,
If you are on the command line I suggest the use of the tail command.
tail -n 123 filename
If you must perform this operation in perl then do a google search for
perl tail. You will see that the perl solution is vastly more
complicated than the above.
--
Kind Regards,
Keith
| |
| Errin M HMMA/Information Technology Department Lar 2005-07-25, 5:30 pm |
| Edward WIJAYA wrote:
> On Mon, 25 Jul 2005 21:39:50 +0800, <kaushik.m@tcs.com> wrote:
>=20
>=20
>=20
>=20
> If you are under unix/linux
> just use "tail -n" command.
>=20
> However if you really want to go via Perl command line:
>=20
> $perl -e '
> open FH, "<file" or die $!;
> @lines =3D <FH>;
> close FH or die $!;
> foreach $i (($#lines - $n) .. $#lines) { print "$lines[$i]\n";} '
>=20
I shortened this up a bit:
perl -e 'open FH, "$filename"; @lines=3Dreverse <FH>; print $lines[$_]
foreach reverse 0..$n;'
Where "$filename" is replaced with the name of the file and "$n" is
replaced with the number of lines to display.
Hope that helps!
--Errin
| |
| Matthias Ochs 2005-07-25, 5:30 pm |
| kaushik.m@tcs.com wrote:
> hi ,
> I am a perl newbie.
> Can someone suggest a perl command line snippet that will print the last n
> lines of a file.
>
Why do you want a Perl line for that?
You could just use 'tail' (assuming you run some kind of UNIX).
Matthias
| |
| Jeff 'japhy' Pinyan 2005-07-25, 5:30 pm |
| On Jul 25, kaushik.m@tcs.com said:
> Can someone suggest a perl command line snippet that will print the last n
> lines of a file.
The File::ReadBackwards module does it for you rather simply.
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
| |
| Errin M HMMA/Information Technology Department Lar 2005-07-25, 5:30 pm |
| Dave Adams wrote:
> Larsen,
>=20
Hi Dave. My name is Errin. Larsen is my surname.
Please, when posting replies, post to the list.
> I am afraid I cannot get your suggested code to work. Especially
> line that reads "foreach reverse 0..$n;"=20
<SNIP>
[color=darkred]
>=20
> On 7/25/05, Larsen, Errin M HMMA/Information Technology Department
> <errinlarsen@hmmausa.com> wrote:=20
$lines[$_][color=darkred]
First, can you tell us about your environment? UNIX or Windows? Other
OS? What version of Perl? Please give us more details.
Second, you can try making the line more "correct":
perl -e 'open FH, "<$filename"; @lines=3Dreverse(<FH> );
print($lines[$_]) foreach(reverse(0..$n));'
See how that line does.
Third, as I mentioned, the '$n' part of that command line needs to be
replaced with a number, AND the '$filename' part of that command line
needs to be replaced with a file name. If I have a file named
'foo.bar', and I want to see the last 25 lines of it, I would type:
perl -e 'open FH, "foo.bar"; @lines=3Dreverse FH; print $lines[$_]
foreach reverse 0..25;'
I hope that is helpful!
--Errin
PS I just re-read you question (above) and I realized maybe you hadn't
realized the code I posted was all on one command line. Make sure you
don't hit 'enter' until the entire command line is typed in.
=20
| |
| John W. Krahn 2005-07-25, 10:03 pm |
| kaushik.m@tcs.com wrote:
> hi ,
Hello,
> I am a perl newbie.
> Can someone suggest a perl command line snippet that will print the last n
> lines of a file.
perl -ne'INIT{$#x=shift()-1}@x=(splice(@x,1),$_)}{print@x' 4 yourfile
John
| |
| FreeFall 2005-07-25, 10:03 pm |
| try:
perl -ne '$line=$_;END{print $line}' yourfile
On Mon, 25 Jul 2005 19:09:50 +0530
kaushik.m@tcs.com wrote:
>
> hi ,
> I am a perl newbie.
> Can someone suggest a perl command line snippet that will print the last n
> lines of a file.
>
> thanks in advance.
> regards,
> Kaushik
>
> Notice: The information contained in this e-mail message and/or attachments to it may contain confidential or privileged information. If you are not the intended recipient, any dissemination, use, review, distribution, printing or copying of the infor
mation contained in this e-mail message and/or attachments to it are strictly prohibited. If you have received this communication in error, please notify us by reply e-mail or telephone and immediately and permanently delete the message and any attachme
nts. Thank you
--
Whatever you do will be insignificant,but
the important is you do it!
It doesn't matter who you are, it's what
you do that takes you far!
| |
| Jay Savage 2005-07-26, 5:00 pm |
| On 7/25/05, FreeFall <renql@ipp.ac.cn> wrote:[color=darkred]
> try:
> perl -ne '$line=3D$_;END{print $line}' yourfile
>=20
> On Mon, 25 Jul 2005 19:09:50 +0530
> kaushik.m@tcs.com wrote:
>=20
t n[color=darkred]
perl -e'$n=3Dshift; @x=3D<>; print splice(@x, -$n), "\n"' 123 yourfile=20
but really, on the command line you're better off just using tail if
you have it.
HTH,
-- jay=20
--------------------------------------------------
This email and attachment(s): [ x ] blogable; [ ] ask first; [ ]
private and confidential
daggerquill [at] gmail [dot] com
http://www.tuaw.com http://www.dpguru.com http://www.engatiki.org
|
|
|
|
|