在 bash 中计算(非空白)代码行

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

count (non-blank) lines-of-code in bash

bashunixcountlinesnonblank

提问by Jonathan Hartley

In Bash, how do I count the number of non-blank lines of code in a project?

在 Bash 中,如何计算项目中非空代码行的数量?

回答by Michael Cramer

cat foo.c | sed '/^\s*$/d' | wc -l

And if you consider comments blank lines:

如果您考虑评论空白行:

cat foo.pl | sed '/^\s*#/d;/^\s*$/d' | wc -l

Although, that's language dependent.

虽然,这取决于语言。

回答by Gilles

#!/bin/bash
find . -path './pma' -prune -o -path './blog' -prune -o -path './punbb' -prune -o -path './js/3rdparty' -prune -o -print | egrep '\.php|\.as|\.sql|\.css|\.js' | grep -v '\.svn' | xargs cat | sed '/^\s*$/d' | wc -l

The above will give you the total count of lines of code (blank lines removed) for a project (current folder and all subfolders recursively).

以上将为您提供项目(当前文件夹和所有子文件夹递归)的代码行总数(删除了空白行)。

In the above "./blog" "./punbb" "./js/3rdparty" and "./pma" are folders I blacklist as I didn't write the code in them. Also .php, .as, .sql, .css, .js are the extensions of the files being looked at. Any files with a different extension are ignored.

在上面的“./blog”“./punbb”“./js/3rdparty”和“./pma”是我黑名单的文件夹,因为我没有在其中编写代码。此外 .php、.as、.sql、.css、.js 是正在查看的文件的扩展名。任何具有不同扩展名的文件都将被忽略。

回答by xsl

If you want to use something other than a shell script, try CLOC:

如果你想使用 shell 脚本以外的东西,试试CLOC

cloc counts blank lines, comment lines, and physical lines of source code in many programming languages. It is written entirely in Perl with no dependencies outside the standard distribution of Perl v5.6 and higher (code from some external modules is embedded within cloc) and so is quite portable.

cloc 计算许多编程语言中源代码的空行、注释行和物理行数。它完全用 Perl 编写,在 Perl v5.6 及更高版本的标准发行版之外没有任何依赖项(来自一些外部模块的代码嵌入在 cloc 中),因此非常可移植。

回答by SpoonMeiser

There are many ways to do this, using common shell utilities.

有很多方法可以使用通用的 shell 实用程序来做到这一点。

My solution is:

我的解决办法是:

grep -cve '^\s*$' <file>

This searches for lines in <file> the do not match (-v) lines that match the pattern (-e) '^\s*$', which is the beginning of a line, followed by 0 or more whitespace characters, followed by the end of a line (ie. no content other then whitespace), and display a count of matching lines (-c) instead of the matching lines themselves.

这将在 <file> 中搜索与模式 (-e) '^\s*$' 匹配的不匹配 (-v) 行,它是一行的开头,后跟 0 个或多个空白字符,然后是在一行的末尾(即除了空格之外没有内容),并显示匹配行数(-c)而不是匹配行本身。

An advantage of this method over methods that involve piping into wc, is that you can specify multiple files and get a separate count for each file:

与涉及管道输入的方法相比,此方法的一个优点wc是您可以指定多个文件并为每个文件获得单独的计数:

$ grep -cve '^\s*$' *.hh

config.hh:36
exceptions.hh:48
layer.hh:52
main.hh:39

回答by Jonathan Hartley

'wc' counts lines, words, chars, so to count all lines (including blank ones) use:

'wc' 计算行数、单词数、字符数,因此要计算所有行(包括空白行),请使用:

wc *.py

To filter out the blank lines, you can use grep:

要过滤掉空行,您可以使用 grep:

grep -v '^\s*$' *.py | wc

'-v' tells grep to output all lines except those that match '^' is the start of a line '\s*' is zero or more whitespace characters '$' is the end of a line *.py is my example for all the files you wish to count (all python files in current dir) pipe output to wc. Off you go.

'-v' 告诉 grep 输出除匹配的所有行 '^' 是行的开头 '\s*' 是零个或多个空白字符 '$' 是行的结尾 *.py 是我的示例您希望计算的所有文件(当前目录中的所有 python 文件)通过管道输出到 wc。就行了。

I'm answering my own (genuine) question. Couldn't find an stackoverflow entry that covered this.

我正在回答我自己的(真正的)问题。找不到涵盖此内容的 stackoverflow 条目。

回答by coastline

This command count number of non-blank lines.
cat fileName | grep -v ^$ | wc -l
grep -v ^$ regular expression function is ignore blank lines.

此命令计算非空行的数量。
cat fileName | grep -v ^$ | wc -l
grep -v ^$ 正则表达式函数是忽略空行。

回答by curtisk

cat 'filename' | grep '[^ ]' | wc -l

should do the trick just fine

应该可以很好地解决问题

回答by Jaydillan

cat file.txt | awk 'NF' | wc -l

回答by Ben Hoffstein

awk '/^[[:space:]]*$/ {++x} END {print x}' "$testfile"

回答by sami

grep -cvE '(^\s*[/*])|(^\s*$)' foo

-c = count
-v = exclude
-E = extended regex
'(comment lines) OR (empty lines)'
where
^    = beginning of the line
\s   = whitespace
*    = any number of previous characters or none
[/*] = either / or *
|    = OR
$    = end of the line

I post this becaus other options gave wrong answers for me. This worked with my java source, where comment lines start with / or * (i use * on every line in multi-line comment).

我发布这个是因为其他选项给了我错误的答案。这适用于我的 java 源代码,其中注释行以 / 或 * 开头(我在多行注释中的每一行都使用 *)。