我想计算$
具有多个子目录的目录中每个文件中的所有字符。我的目标是计算 PHP 项目中的所有变量。文件有后缀.php
。
我试过
grep -r '$' . | wc -c
grep -r '$' . | wc -l
还有很多其他的东西,但都返回了一个无法匹配的数字。在我的示例文件中只有四个$
. 所以我希望有人能帮助我。
编辑
我的示例文件
<?php
class MyClass extends Controller {
$a;$a;
$a;$a;
$a;
$a;
我想计算$
具有多个子目录的目录中每个文件中的所有字符。我的目标是计算 PHP 项目中的所有变量。文件有后缀.php
。
我试过
grep -r '$' . | wc -c
grep -r '$' . | wc -l
还有很多其他的东西,但都返回了一个无法匹配的数字。在我的示例文件中只有四个$
. 所以我希望有人能帮助我。
编辑
我的示例文件
<?php
class MyClass extends Controller {
$a;$a;
$a;$a;
$a;
$a;
要递归计算$
目录中一组文件中的字符数,您可以执行以下操作:
fgrep -Rho '$' some_dir | wc -l
要在递归中仅包含扩展文件,.php
您可以改用:
fgrep -Rho --include='*.php' '$' some_dir | wc -l
-R
is 用于递归遍历文件,is用于匹配搜索的每一行some_dir
的-o
一部分。文件集仅限于模式*.php
,并且文件名不包含在输出中-h
,否则可能会导致误报。
要计算 PHP 项目中的变量,您可以使用这里variable regex
定义的.
因此,下一个将 grep 每个文件的所有变量:
cd ~/my/php/project
grep -Pro '\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*' .
-P - use perlish regex
-r - recursive
-o - each match on separate line
会产生类似的东西:
./elFinderVolumeLocalFileSystem.class.php:$path
./elFinderVolumeLocalFileSystem.class.php:$path
./elFinderVolumeMySQL.class.php:$driverId
./elFinderVolumeMySQL.class.php:$db
./elFinderVolumeMySQL.class.php:$tbf
你想计算它们,所以你可以使用:
$ grep -Proc '\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*' .
并会得到count of variables in each file
,比如:
./connector.minimal.php:9
./connector.php:9
./elFinder.class.php:437
./elFinderConnector.class.php:46
./elFinderVolumeDriver.class.php:1343
./elFinderVolumeFTP.class.php:577
./elFinderVolumeFTPIIS.class.php:63
./elFinderVolumeLocalFileSystem.class.php:279
./elFinderVolumeMySQL.class.php:335
./mime.types:0
./MySQLStorage.sql:0
当想要 countby file and by variable
时,您可以使用:
$ grep -Pro '\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*' . | sort | uniq -c
获得如下结果:
17 ./elFinderVolumeLocalFileSystem.class.php:$target
8 ./elFinderVolumeLocalFileSystem.class.php:$targetDir
3 ./elFinderVolumeLocalFileSystem.class.php:$test
97 ./elFinderVolumeLocalFileSystem.class.php:$this
1 ./elFinderVolumeLocalFileSystem.class.php:$write
6 ./elFinderVolumeMySQL.class.php:$arc
3 ./elFinderVolumeMySQL.class.php:$bg
10 ./elFinderVolumeMySQL.class.php:$content
1 ./elFinderVolumeMySQL.class.php:$crop
你可以看到,变量$write
只使用一次,所以(也许)它没用。
你也可以数per variable per whole project
$ grep -Proh '\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*' . | sort | uniq -c
并会得到类似的东西:
13 $tree
1 $treeDeep
3 $trg
3 $trgfp
10 $ts
6 $tstat
35 $type
在哪里你可以看到,比$treeDeep
在整个项目中只使用一次,所以它肯定没用。
grep
您可以使用不同的 ,sort
和命令实现许多其他组合uniq
..