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

Oh dearest wisest Perl Monks, please aid me in this dilemma for I new to Perl and a beast (my boss) is on my heels. I wish to read only the first 10 lines of a text file and then output them. How do I do this. All the books I have read have only tell how to read and write the entire file. Many thanks. -Your humble servant
  • Comment on Reading in the first 10 lines of a file

Replies are listed 'Best First'.
Re: Reading in the first 10 lines of a file
by Tux (Canon) on Dec 10, 2011 at 11:56 UTC

    All post scriplets, but if you just need to read the first 10 lines from the command line, there are shorter ways to achive that goal

    $ head -10 file $ perl -ne'1..10 and print' file

    The advantage of the perl way is that you can also easily get stuff from the middle, say printl only line 12345:

    $ perl -ne'12345..12345 and print' file

    Enjoy, Have FUN! H.Merijn
Re: Reading in the first 10 lines of a file
by davorg (Chancellor) on Feb 02, 2000 at 01:26 UTC
Re: Reading in the first 10 lines of a file
by mortis (Pilgrim) on Feb 02, 2000 at 00:41 UTC
    #!/usr/bin/perl -wT use strict; for(1..10) { print scalar(<>); }
Re: Reading in the first 10 lines of a file
by Anonymous Monk on Feb 01, 2000 at 23:43 UTC
    #!/bin/perl -w # readable and extendable while (<>) { if (1..10) { print $_;} else {last;} }
Re: Reading in the first 10 lines of a file
by Benne (Initiate) on Jan 27, 2000 at 22:30 UTC
    Simply add a counter in the while loop:
    #!usr/bin/perl use strict; open FILE, "<$ARGV[0]"; # $ARGV[0] is the file name # to pass in in the command line. my $i=1; while (<FILE>) { if ($i >10) { last;} print $_ ; $i++; } close FILE;
Re: Reading in the first 10 lines of a file
by dlc (Acolyte) on Jan 28, 2000 at 19:35 UTC
    here's a quick one liner that you can stick in the middle of something (F is an open filehandle, $counter is predefined as 0):
    do { ($counter++ < 11) ? print : last } while (<F>)
    be sure to close F.
Re: Reading in the first 10 lines of a file
by trizen (Hermit) on Dec 10, 2011 at 11:09 UTC
    while (<>) { $. > 10 ? last : print; }
Re: Reading in the first 10 lines of a file
by CountZero (Bishop) on Dec 10, 2011 at 21:24 UTC
    perl -e 'print +(<ARGV>)[0..9]' myfile.txt

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Not recommended! Slurps the entire file into memory...
        Yes, I know.

        Memory is plenty and cheap :) and my fingers are clumsy and lazy :D

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Reading in the first 10 lines of a file
by pvaldes (Chaplain) on Dec 10, 2011 at 20:24 UTC

    or alternatively:

    open my $tenlines, "|head myfile.txt"; print $tenlines;