---
title: "perl faq 学习之统计文件行数"
author: "zhouzhao21"
date: "2009-04-25T00:35:43Z"
tags: []
categories: ["编程语言"]
slug: "perl-faq"
draft: false
lastmod: "2009-04-25T00:53:49Z"
id: 374315
url: "https://kyo-cc.iteye.com/blog/374315"
---

目的：统计指定文件的行数。

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

```

one line code on command line :

```command line

perl -ne "print $. if eof" filename

```

 

更改解决方案参见：
<http://bbs.chinaunix.net/archiver/?tid-521001.html>

原文：
<http://faq.perl.org/perlfaq5.html#How_do_I_count_the_n>