Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

how to print first 5 lines of a file

by Anonymous Monk
on Oct 31, 2001 at 22:40 UTC ( [id://122395]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
(Ovid) Re: how to print first 5 lines of a file
by Ovid (Cardinal) on Oct 31, 2001 at 23:01 UTC

    The $. variable holds the current input line number of the last opened filehandle.

    while ( $. < 5 ) { my $line = <DATA>; print "$. -> $line"; } __DATA__ one two three four five six seven

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: how to print first 5 lines of a file
by MZSanford (Curate) on Oct 31, 2001 at 22:50 UTC
    maybe the following untested solutions. Beware, numbers 2 and 3 are a bit on the silly side. :
    1. perl -pe 'exit if ($. > 5);'
    2. #!/usr/bin/perl -w use strict; my $cnt = 0; while (my $line = <>) { chomp($line); $cnt++; if ($cnt > 5) { next; } else { print "$line\n"; } }
    3. use strict; open(PRINT,"| lp") || die "Could not start lp : $!\n"; while (my $line = <>) { print PRINT $line; last if ($. > 5); } close(PRINT);

    i had a memory leak once, and it ruined my favorite shirt.
Re: how to print first 5 lines of a file
by broquaint (Abbot) on Oct 31, 2001 at 22:57 UTC
    Perl-ish
    perl -e 'print ((<>)[0..4])' filename
    sh-ish
    head -5 filename

    HTH

    broquaint

      I certainly wouldn't recommend the first solution as, although it looks nice, it will read the entire contents of the file(s) into memory before spitting out the first 5 lines. Better to not even read the 6th line (as demonstrated in other items in this thread), much less allocate memory for it. (:

              - tye (but my friends call me "Tye")
Re: how to print first 5 lines of a file
by buckaduck (Chaplain) on Nov 01, 2001 at 01:04 UTC
    Just a minor modification to an earlier solution would allow you to print the first five lines of multiple files:
    perl -pe 'close ARGV if $. == 5' file1 file2 ...

    Update: The code above fails if one if the files is less than 5 lines long! This works better:

    perl -pe 'close ARGV if $. == 5 or eof' file1 file2 ...

    buckaduck

Re: how to print first 5 lines of a file
by jmcnamara (Monsignor) on Nov 01, 2001 at 14:44 UTC

    Since -p generates a while loop you can also do this:
    perl -pe 'last if $. > 5' filename


    --
    John.

Re: how to print first 5 lines of a file
by Aighearach (Initiate) on Nov 01, 2001 at 00:51 UTC
    To print the first 5 lines of a file:
    #!/usr/bin/perl seek( DATA, 0, 0 ); $/ = undef; print <DATA>; __DATA__

    --
    Snazzy tagline here
Re: how to print first 5 lines of a file
by CharlesClarkson (Curate) on Nov 01, 2001 at 03:42 UTC
    for (1 .. 5) { print scalar <>; last if eof; }

    Or, if you're certain there are at least five lines:

    print scalar <> for 1 .. 5;

    Calling the diamond operator <> in scalar context will return 1 line, assuming $/ is set to "\n". (Note - 'while' calls in scalar context while 'foreach' and 'for' call in list context.)



    HTH,
    Charles K. Clarkson
Re: how to print first 5 lines of a file
by pixel (Scribe) on Nov 01, 2001 at 13:50 UTC

    Why does no-one ever use the flip-flop operator for this?

    while (<>) { print if 1 .. 5; }

    Blessed Be
    The Pixel

      Nice, but that still loops over the entire file... I'd exit out of the loop after the fifth line like so:
      while(<>) { 1 .. 5 ? print : last; }

      -Blake

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://122395]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-25 13:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found