用bash脚本统计代码行数

获取单个文件行数

文件:test1.sh

行数:20

方法一

awk '{print NR}' test1.sh|tail -n1

如图所示:

方法二

awk 'END{print NR}' test1.sh

如图所示:

方法三

grep -n "" test1.sh|awk -F: '{print '}|tail -n1

如图所示:

方法四

sed -n '$=' test1.sh

如图所示:

方法五

wc -l test1.sh

如图所示:

方法六

#cat test1.sh |wc -l

如图所示:

获取特定目录所有文件的行数

代码如下:

复制代码
#!/bin/bash

filesCount=0
linesCount=0
function funCount()
{
    for file in ` ls $1 `
    do
        if [ -d $1"/"$file ];then
            funCount $1"/"$file
        else
            declare -i fileLines
            fileLines=`sed -n '$=' $1"/"$file`
            let linesCount=$linesCount+$fileLines
            let filesCount=$filesCount+1
        fi
    done
}

if [ $# -gt 0 ];then
    for m_dir in $@
    do
        funCount $m_dir
    done
else
    funCount "."
fi
echo "filesCount = $filesCount"
echo "linesCount = $linesCount"
复制代码

使用方法:

1、针对本目录

./count.sh

2、统计多个目录

./count.sh /tmp ~

运行效果如下:

获取特定目录特定扩展名文件的行数

代码如下:

复制代码
#!/bin/bash

extens=(".c" ".cpp" ".h" ".hpp")

filesCount=0
linesCount=0
function funCount()
{
    for file in ` ls $1 `
    do
        if [ -d $1"/"$file ];then
            funCount $1"/"$file
        else
            fileName=$1"/"$file
            
            EXTENSION="."${fileName##*.}
            echo "fileName = $fileName  Extension = $EXTENSION"

            if [[ "${extens[@]/$EXTENSION/}" != "${extens[@]}" ]];then
                declare -i fileLines
                fileLines=`sed -n '$=' $fileName`
                echo $fileName" : "$fileLines
                let linesCount=$linesCount+$fileLines
                let filesCount=$filesCount+1
            fi
        fi
    done
}

if [ $# -gt 0 ];then
    for m_dir in $@
    do
        funCount $m_dir
    done
else
    funCount "."
fi
echo "filesCount = $filesCount"
echo "linesCount = $linesCount"
复制代码

 使用方法:

1、针对本目录

./count.sh

2、统计多个目录

./count.sh /tmp ~

运行效果如下:

好,就这些了,希望对你有帮助。

 

出处:https://www.cnblogs.com/MikeZhang/archive/2012/08/22/linesCountBash20120822.html

=======================================================================================

代码行数统计工具

使用工具统计代码行

代码行数统计工具 SourceCounter,很好用的。

批处理统计代码行数

1、只统计当前文件夹下的cpp文件的行数,并且不包括空行

find /v /c " " *.cpp >> sum.txt

摘自:http://my.oschina.net/solee/blog/75763

 

2、遍历文件夹下的子目录,输出行数总和

@echo off
REM 关闭命令行提示输出,只输出echo的信息
REM 打印每个文件的行数
find /v /c " " *.h*
find /v /c " " *.c*
REM 统计所有文件总行数
REM 无奈在里面增加代码显示不鸟单个文件的行数
REM /s参数:可以为子目录 /b参数:文件
for /f "delims=" %%i in ('dir/b/s *.h* *.c*') do (
    REM 统计单个文件的行数
    for /f "usebackq" %%j in ("%%i") do  set /a n=n+1
) 
REM 打印总行数
echo 总行数:%n%
REM 结束
pause

 

 

出处:https://www.cnblogs.com/ant-wjf/p/3529935.html

=======================================================================================

个人使用

@echo off&setlocal enabledelayedexpansion
cls
rem 指定文件扩展名,多个时使用空格分隔
set fileExt=*.cs *.h *.c
set fileCount=0
rem 保存文件的所有行数
set linesCountAll=0
rem 保存文件的行数,并不包括空行
set linesCountExcludeNull=0


set codePath=%~1
if [%1] equ [] set codePath=%cd%
rem Format data:remove '' and "" and \
set codePath='%codePath%' & set codePath=%codePath:'=%
set codePath="%codePath%" & set codePath=%codePath:"=%
if "%codePath:~-1%" equ "\" set codePath=%codePath:~,-1%
echo codePath=%codePath%

rem 判断源是文件夹还是文件
set pathIsDir=0
pushd "%codePath%" 2>nul && (popd&set pathIsDir=1)
echo pathIsDir=%pathIsDir%

echo;&echo [%date% %time%]本程序将计算【"%codePath%"】路径下所有【%fileExt%】类型文件的行数.
echo;&echo [%date% %time%]按任意键将继续,退出请输入exit
set varConfim=0&set /p varConfim=
if /i "[%varConfim%]"=="[exit]" goto :eof
if /i [%pathIsDir%]==[0] goto calcFileFun
::goto showResultFun


:calcDirFun
pushd %cd%&cd /d "%codePath%"
echo [%date% %time%]开始计算【"%cd%"】路径下所有【%fileExt%】类型文件的行数.
for /f "delims=" %%i in ('dir/b/s %fileExt%') do (
    set/a fileCount=!fileCount!+1
    ::echo %%i
    rem 可以先使用find /c /v "" "%%i"查看下行数的是第几列,然后再修改tokens的值为几。这里tokens表示取第几列的内容
    for /f "tokens=3 delims=:" %%a in ('find /c /v "" "%%i"') do set/a linesCountAll=!linesCountAll!+%%a
    for /f "usebackq" %%j in ("%%i") do set /a linesCountExcludeNull=!linesCountExcludeNull!+1
)
popd
goto showResultFun


:calcFileFun
echo [%date% %time%]工作目录【%cd%】
if not exist "%codePath%" echo [%date% %time%]文件不存在【"%codePath%"】&goto :eof
echo [%date% %time%]开始计算【"%codePath%"】文件的行数.
for /f "delims=" %%i in ('dir/b/s "%codePath%"') do (
    set/a fileCount=!fileCount!+1
    ::echo %%i
    rem 可以先使用find /c /v "" "%%i"查看下行数的是第几列,然后再修改tokens的值为几。这里tokens表示取第几列的内容
    for /f "tokens=3 delims=:" %%a in ('find /c /v "" "%%i"') do set/a linesCountAll=!linesCountAll!+%%a
    for /f "usebackq" %%j in ("%%i") do set /a linesCountExcludeNull=!linesCountExcludeNull!+1
)
goto showResultFun


:showResultFun
echo.
if /i [%pathIsDir%]==[0] echo [%date% %time%]完成计算【"%codePath%"】文件的行数.
if /i [%pathIsDir%]==[1] echo [%date% %time%]完成计算【"%codePath%"】路径下所有【%fileExt%】类型文件的行数.
echo [%date% %time%]找到所有文件总数:%fileCount% 个
echo [%date% %time%]所有文件的行总数:%linesCountAll% 行
echo [%date% %time%]去除空行的行总数:%linesCountExcludeNull% 行
echo.
echo.
pause
View Code

 

posted on 2022-10-22 22:16  jack_Meng  阅读(434)  评论(0编辑  收藏  举报

导航