bash 使用 find、wc 和 sed 计算行数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1412244/
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-09-09 18:31:22  来源:igfitidea点击:

Use find, wc, and sed to count lines

bashsedfindwc

提问by Berlin Brown

I was trying to use sed to count all the lines based on a particular extension.

我试图使用 sed 来计算基于特定扩展名的所有行。

find -name '*.m' -exec wc -l {} \; | sed ...

I was trying to do the following, how would I include sed in this particular line to get the totals.

我正在尝试执行以下操作,如何在此特定行中包含 sed 以获取总数。

回答by Emmanuel BERNAT

You may also get the nice formatting from wc with :

您还可以使用以下命令从 wc 获得漂亮的格式:

wc `find -name '*.m'`

回答by Daniel James

Most of the answers here won't work well for a large number of files. Some will break if the list of file names is too long for a single command line call, others are inefficient because -execstarts a new process for every file. I believe a robust and efficient solution would be:

这里的大多数答案不适用于大量文件。如果文件名列表对于单个命令行调用来说太长,有些会中断,有些则效率低下,因为-exec为每个文件启动一个新进程。我相信一个强大而有效的解决方案是:

find . -type f -name "*.m" -print0 | xargs -0 cat | wc -l

Using catin this way is fine, as its output is piped straight into wcso only a small amount of the files' content is kept in memory at once. If there are too many files for a single invocation of cat, catwill be called multiple times, but all the output will still be piped into a single wcprocess.

cat以这种方式使用很好,因为它的输出是直接通过管道输入的,wc因此一次只有少量文件内容保存在内存中。如果单个调用有太多文件catcat将被多次调用,但所有输出仍将通过管道传输到单个wc进程中。

回答by sth

You can catall files through a single wcinstance to get the total number of lines:

您可以cat通过单个wc实例获取所有文件的总行数:

find . -name '*.m' -exec cat {} \; | wc -l

回答by Alexei Polkhanov

On modern GNU platforms wc and find take -print0 and -files0-from parameters that can be combined into a command that count lines in files with total at the end. Example:

在现代 GNU 平台上 wc 和 find 可以将 -print0 和 -files0-from 参数组合成一个命令,该命令在文件中以总行数结尾。例子:

find . -name '*.c' -type f -print0 | wc -l --files0-from=-

回答by dfa

you could use sed also for counting lines in place of wc:

您也可以使用 sed 代替 wc 来计算行数:

 find . -name '*.m' -exec sed -n '$=' {} \;

where '$='is a "special variable" that keep the count of lines

哪里'$='是保持行数的“特殊变量”

EDIT

编辑

you could also try something like sloccount

你也可以尝试像sloccount这样的东西

回答by igustin

Hm, solution with catmay be problematic if you have many files, especially big ones.

嗯,如果你有很多文件,尤其是大文件,用cat 的解决方案可能会有问题。

Second solution doesn't give total, just lines per file, as I tested.

正如我测试的那样,第二个解决方案没有给出总数,只是每个文件的行数。

I'll prefer something like this:

我会更喜欢这样的:

find . -name '*.m' | xargs wc -l | tail -1

This will do the job fast, no matter how many and how big files you have.

无论您拥有多少和多大的文件,这都可以快速完成工作。

回答by marco

sed is not the proper tool for counting. Use awk instead:

sed 不是正确的计数工具。改用 awk:

find . -name '*.m' -exec awk '{print NR}' {} +

Using + instead of \; forces find to call awk every N files found (like with xargs).

使用 + 代替 \; 强制 find 调用 awk 每找到 N 个文件(如使用 xargs)。

回答by marco

For big directories we should use:

对于大目录,我们应该使用:

find . -type f -name '*.m' -exec sed -n '$=' '{}' + 2>/dev/null | awk '{ total+= }END{print total}' 

# alternative using awk twice
find . -type f -name '*.m' -exec awk 'END {print NR}' '{}' + 2>/dev/null | awk '{ total+= }END{print total}'