http://www.perlmonks.org?node_id=28301

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

How do I find the total number of lines in a file?

Originally posted as a Categorized Question.

  • Comment on How do I find the total number of lines in a file?

Replies are listed 'Best First'.
Re: How do I find the total number of lines in a file?
by nuance (Hermit) on Aug 17, 2000 at 19:42 UTC
    The variable $. contains the line number of the last file read from, so:
    for (<FILEHANDLE>); print "FILEHANDLE contains $. lines";
    note: If you're using <> to iterate over @ARGV then the line numbers don't reset over the files. So on if you were reading five ten line files on line five of file three, $. will contain 25.

    See perldoc perlvar for more info

    Editor's Note:
    $. is reset on an explicit close. Opening an open filehandle first does an implicit close on it, then opens it. This does NOT reset $. to zero. This is one of the many reasons to explicitly close filehandles when you are done with them. <> Uses the default filehandle and does not explicitly close it, which is why you get the behaviour that nuance mentioned. Of course, you can also set $. yourself as a point to start counting from (although this does NOT tell the read operator to start at the specified line, $. is just a counter) and you can scope $. with local $. as well.

Re: How do I find the total number of lines in a file?
by KM (Priest) on Aug 17, 2000 at 19:38 UTC
    From a shell you can:

    wc -l file

    From Perl you can read the file into an array, then use the array in scalar context to get the number of lines. Or, you could loop through and count, something like:

    my $cnt; open(FH,"file") or die "Damn. $!"; $cnt++ while <FH>; close FH;

    Or, use $. (perldoc perlvar)

    while (<FH>) {} print $.;

    Or, to skip blank lines...

    open(FH,"calendar.cgi") or die "Damn. $!"; while (<FH>) { $cnt++ if !/^\s+?$/;} close FH;

    Cheers,
    KM

Re: How do I find the total number of lines in a file?
by fundflow (Chaplain) on Aug 17, 2000 at 19:42 UTC
    You can use the $. variable ($/ defaults to \n)
    while(<>){}; print "$. lines\n";
Re: How do I find the total number of lines in a file?
by Juerd (Abbot) on Jan 01, 2002 at 22:06 UTC
    This is a FAQ, answered in perlfaq5.

    I quote from that document:

    How do I count the number of lines in a file?

    One fairly efficient way is to count newlines in the file. The following program uses a feature of tr///, as documented in perlop. If your text file doesn't end with a newline, then it's not really a proper text file, so this may report one fewer line than you expect.

    $lines = 0; open(FILE, $filename) or die "Can't open `$filename': $!"; while (sysread FILE, $buffer, 4096) { $lines += ($buffer =~ tr/\n//); } close FILE;

    This assumes no funny games with newline translations.



    Read the FAQs, memorize the questions, so you know where to look when you need the answer.
    perldoc Is a handy tool. perldoc -q count or perldoc -q lines would have given you the answer you wanted.
Re: How do I find the total number of lines in a file?
by jospan (Novice) on Nov 08, 2010 at 20:33 UTC
    In an AWK-ish kind of way, straight from the command prompt: perl -lne "END { print $. }" myFilename
Re: How do I find the total number of lines in a file?
by Nooks (Monk) on Aug 22, 2000 at 09:36 UTC
    Try:
    my $total = @{[<FILE>]}; print $total,"\n";
Re: How do I find the total number of lines in a file?
by newbie00 (Beadle) on Jan 01, 2002 at 01:21 UTC
    Try:
    open(FILE, "filename"); @lines = <FILE>; close(FILE); $num = @lines;

    --newbie00

      @lines could get rather large depending on the size of the file; how about
      open(FILE, "filename") or die "couldn't open filename: $!"; # check re +turn status of system calls! for ($count = 0; <FILE>; $count++) {}
      Josh
Re: How do I find the total number of lines in a file?
by jospan (Novice) on Nov 13, 2010 at 09:31 UTC
    In an AWK-ish kind of way, straight from the command prompt: perl -lne "END { print $. }" myFilename
Re: How do I find the total number of lines in a file?
by spacemunky (Initiate) on Aug 23, 2000 at 00:09 UTC
    cat -n

    Originally posted as a Categorized Answer.

Re: How do I find the total number of lines in a file?
by JavaFan (Canon) on Nov 13, 2010 at 14:55 UTC
    My favourite method to get the number of lines in a file is:
    $line_count = `wc -l < $file`;
    The redirection there is on purpose, without the < there, you get a different answer.