Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

0 views
Skip to first unread message

PerlFAQ Server

unread,
Dec 16, 2009, 6:00:03 AM12/16/09
to
This is an excerpt from the latest version perlfaq5.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

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

--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.

C.DeRykus

unread,
Dec 16, 2009, 11:06:08 AM12/16/09
to
On Dec 16, 3:00 am, PerlFAQ Server <br...@theperlreview.com> wrote:
> This is an excerpt from the latest version perlfaq5.pod, which
> comes with the standard Perl distribution. These postings aim to
> reduce the number of repeated questions as well as allow the community
> to review and update the answers. The latest version of the complete
> perlfaq is athttp://faq.perl.org.
>
> --------------------------------------------------------------------
>
> 5.3: 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.
>
> --------------------------------------------------------------------
>

Wasn't one of these one-liners included once?

perl -lne '}print $.;{' file # Abigail's trick
perl -lne 'END {print $.}' file

--
Charles DeRykus


brian d foy

unread,
Dec 16, 2009, 4:10:47 PM12/16/09
to
In article
<48ffe7ef-f5da-4ec4...@u36g2000prn.googlegroups.com>,
C.DeRykus <der...@gmail.com> wrote:

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

> Wasn't one of these one-liners included once?
>
> perl -lne '}print $.;{' file # Abigail's trick
> perl -lne 'END {print $.}' file

Those seem like worthy additions. I'll add them to my queue.

0 new messages