我有这样的线,我想知道我有多少行。

09:16:39 AM  all    2.00    0.00    4.00    0.00    0.00    0.00    0.00    0.00   94.00
09:16:40 AM  all    5.00    0.00    0.00    4.00    0.00    0.00    0.00    0.00   91.00
09:16:41 AM  all    0.00    0.00    4.00    0.00    0.00    0.00    0.00    0.00   96.00
09:16:42 AM  all    3.00    0.00    1.00    0.00    0.00    0.00    0.00    0.00   96.00
09:16:43 AM  all    0.00    0.00    1.00    0.00    1.00    0.00    0.00    0.00   98.00
09:16:44 AM  all    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
09:16:45 AM  all    2.00    0.00    6.00    0.00    0.00    0.00    0.00    0.00   92.00

是否有一种方法可以使用linux命令来计算它们?


当前回答

wc -l <file.txt>

Or

command | wc -l

其他回答

像这样使用nl:

nl filename

来自man nl:

将每个FILE写入标准输出,并添加行号。与 没有FILE,或者当FILE为-时,读取标准输入。

我知道这很老,但仍然是:数过滤后的行数

我的文件如下:

Number of files sent
Company 1 file: foo.pdf OK
Company 1 file: foo.csv OK
Company 1 file: foo.msg OK
Company 2 file: foo.pdf OK
Company 2 file: foo.csv OK
Company 2 file: foo.msg Error
Company 3 file: foo.pdf OK
Company 3 file: foo.csv OK
Company 3 file: foo.msg Error
Company 4 file: foo.pdf OK
Company 4 file: foo.csv OK
Company 4 file: foo.msg Error

如果我想知道发送了多少文件

grep "OK" <filename> | wc -l

OR

grep -c "OK" filename

如果你想要的只是行数(而不是行数和返回的愚蠢的文件名):

wc -l < /filepath/filename.ext

正如前面提到的,这些方法也有效(但由于其他原因,效果较差):

awk 'END{print NR}' file       # not on all unixes
sed -n '$=' file               # (GNU sed) also not on all unixes
grep -c ".*" file              # overkill and probably also slower

Awk节省了生命时间(和行):

awk '{c++};END{print c}' < file

如果你想确保你没有计算空行,你可以这样做:

awk '{/^./ && c++};END{print c}' < file

正如其他人所说,wc -l是最好的解决方案,但为了将来参考,你可以使用Perl:

perl -lne 'END { print $. }'

美元。包含行号和END块将在脚本的末尾执行。