Click here to get back home

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

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
FAQ 5.3 How do I count the number of lines in a file? PerlFAQ Server 04-19-2008
Posted by PerlFAQ Server on April 19, 2008, 3:03 am
Please log in for more thread options
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.

Posted by szr on April 19, 2008, 3:26 am
Please log in for more thread options
PerlFAQ Server wrote:
[...]
> 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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Shouldn't this be "If the lines in your text file do not end with a
newline", or something to that effect? A text file need not have a "\n"
at the very end :)

> not really a proper text file, so this may report one fewer line
> than you expect.
[...]

--
szr



Posted by Jürgen Exner on April 19, 2008, 6:08 am
Please log in for more thread options
>PerlFAQ Server wrote:
>[...]
>> 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
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
>Shouldn't this be "If the lines in your text file do not end with a
>newline", or something to that effect?

No. It is a different way of saying "If there is stuff after the last
newline then that stuff doesn't constitute a proper line..."

>A text file need not have a "\n"
>at the very end :)
>
>> not really a proper text file, so this may report one fewer line
>> than you expect.

"... and therefore it is not be counted as a line."

jue

Posted by szr on April 19, 2008, 6:05 pm
Please log in for more thread options
Jürgen Exner wrote:
>> PerlFAQ Server wrote:
>> [...]
>>> 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
>> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>
>> Shouldn't this be "If the lines in your text file do not end with a
>> newline", or something to that effect?
>
> No. It is a different way of saying "If there is stuff after the last
> newline then that stuff doesn't constitute a proper line..."

I don't recall there being any sort of rule saying a text file's last
character /has/ to be a line terminator, though I can see how it would
seem better formed if it does.

--
szr



Posted by brian d foy on April 20, 2008, 4:07 pm
Please log in for more thread options
wrote:

> Jürgen Exner wrote:

> > No. It is a different way of saying "If there is stuff after the last
> > newline then that stuff doesn't constitute a proper line..."
>
> I don't recall there being any sort of rule saying a text file's last
> character /has/ to be a line terminator, though I can see how it would
> seem better formed if it does.

There's no rule for it, but a lot of things expect it. Just Google
"no newline at end of file" to see for yourelf.

Similar ThreadsPosted
FAQ 5.3: How do I count the number of lines in a file? December 4, 2004, 6:03 am
FAQ 5.3: How do I count the number of lines in a file? January 6, 2005, 12:03 am
FAQ 5.3 How do I count the number of lines in a file? January 30, 2005, 12:03 pm
FAQ 5.3 How do I count the number of lines in a file? May 13, 2005, 11:03 pm
FAQ 5.3 How do I count the number of lines in a file? July 29, 2005, 10:03 am
FAQ 5.3 How do I count the number of lines in a file? September 12, 2005, 4:03 am
FAQ 5.3 How do I count the number of lines in a file? November 14, 2005, 11:03 pm
FAQ 5.3 How do I count the number of lines in a file? April 23, 2006, 3:03 pm
FAQ 5.3 How do I count the number of lines in a file? August 8, 2006, 3:03 pm
FAQ 5.3 How do I count the number of lines in a file? November 17, 2006, 9:03 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap