Linux 如何计算文档中的行数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3137094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:11:17  来源:igfitidea点击:

How to count lines in a document?

linuxbashcommand-linescripting

提问by Alucard

I have lines like these, and I want to know how many lines I actually have...

我有这样的行,我想知道我实际上有多少行...

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

Is there a way to count them all using linux commands?

有没有办法使用 linux 命令来计算它们?

采纳答案by user85509

Use wc:

使用wc

wc -l <filename>

This will output the number of lines in <filename>:

这将输出中的行数<filename>

$ wc -l /dir/file.txt
3272485 /dir/file.txt

Or, to omit the <filename>from the result use wc -l < <filename>:

或者,<filename>要从结果中省略 ,请使用wc -l < <filename>

$ wc -l < /dir/file.txt
3272485

You can also pipe data to wcas well:

您还可以将数据通过管道传输到wc

$ cat /dir/file.txt | wc -l
3272485
$ curl yahoo.com --silent | wc -l
63

回答by John Kugelman

wc -l <file.txt>

Or

或者

command | wc -l

回答by Vivin Paliath

Use wc:

使用wc

wc -l <filename>

回答by Lauro Oliveira

To count all lines use:

要计算所有行,请使用:

$ wc -l file

To filter and count only lines with pattern use:

要仅过滤和计算具有模式的行,请使用:

$ grep -w "pattern" -c file  

Or use -v to invert match:

或使用 -v 反转匹配:

$ grep -w "pattern" -c -v file 

See the grep man page to take a look at the -e,-i and -x args...

请参阅 grep 手册页以了解 -e、-i 和 -x 参数...

回答by ghostdog74

there are many ways. using wcis one.

有很多方法。使用wc是一种。

wc -l file

wc -l file

others include

其他包括

awk 'END{print NR}' file

awk 'END{print NR}' file

sed -n '$=' file(GNU sed)

sed -n '$=' file(GNU sed)

grep -c ".*" file

回答by built1n

The tool wcis the "word counter" in UNIX and UNIX-like operating systems, you can also use it to count lines in a file, by adding the -loption, so wc -l foowill count the number of lines in foo. You can also pipe output from a program like this: ls -l | wc -l, which will tell you how many files are in the current directory.

该工具wc是 UNIX 和类 UNIX 操作系统中的“字计数器”,您还可以使用它来计算文件中的行数,通过添加-l选项,wc -l foo将计算 .csv 文件中的行数foo。您还可以通过管道输出这样的程序:ls -l | wc -l,它会告诉您当前目录中有多少文件。

回答by decimal

Use nllike this:

nl像这样使用:

nl filename

From man nl:

来自man nl

Write each FILE to standard output, with line numbers added. With no FILE, or when FILE is -, read standard input.

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

回答by ggb667

If all you want is the number of lines (and not the number of lines and the stupid file name coming back):

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

wc -l < /filepath/filename.ext

As previously mentioned these also work (but are inferior for other reasons):

如前所述,这些也有效(但由于其他原因而较差):

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

回答by Majid Azimi

As others said wc -lis the best solution, but for future reference you can use Perl:

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

perl -lne 'END { print $. }'

$.contains line number and ENDblock will execute at the end of script.

$.包含行号,END块将在脚本结束时执行。

回答by Yogi

Above are the preferred method but "cat" command can also helpful:

以上是首选方法,但“cat”命令也有帮助:

cat -n <filename>

Will show you whole content of file with line numbers.

将显示带有行号的文件的全部内容。