Archive for 'Perl'

Count number of lines in text file using perl

The best way to do that is to count the number of new line characters in the file. An example for the same is given below:

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

If your text file doesn’t end with a new …

Read the complete article »