Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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

by faq_monk (Initiate)
on Oct 08, 1999 at 00:23 UTC ( #646=perlfaq nodetype: print w/replies, xml ) Need Help??

Current Perl documentation can be found at perldoc.perl.org.

Here is our local, out-dated (pre-5.6) version:

One fairly efficient way is to count newlines in the file. The following program uses a feature of tr///, as documented in the perlop manpage. 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
[thezip]: G'day all. Apparently Strawberry Perl has issues with deploying Spreadsheet::XLSX for Perl 5.24.1. I need this module to maintain compatability with an existing script. I'm new to Strawberry -- is there a safe way to manually deploy this?
[MidLifeXis]: Where is it hanging up?
[MidLifeXis]: ISTR having a couple of issue based on undeclared dependencies around the time I was looking at that module on my last project, but cannot say if that was the module that I had the dependency issues with.
[MidLifeXis]: (5.22 at that time)
[MidLifeXis]: Yeah, I know, really helpful :-)
[Corion]: If all else fails, Spreadsheet::XLSX should be "installable" by manually copying the files, provided that the prerequisites are met
[thezip]: It fails in the "formatted 2-digit numeric" tests
[thezip]: Okay Corion, that's what I wanted to hear. Thanks!
[Corion]: Oh, a test failure... Just force-install it then? --force and potentially --notest to skip the tests alltogether ?
[ambrus]: thezip: is Activestate Perl usable for your scenario instead?

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (13)
As of 2017-03-23 17:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (290 votes). Check out past polls.