天天看點

Shell--腳本統計檔案行數

示例:row_count.sh檔案

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

awk 'END{print NR}' row_count.sh

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

sed -n '$=' row_count.sh

wc -l row_count.sh

cat row_count.sh | wc -l
           
Shell--腳本統計檔案行數

一、擷取指定目錄所有檔案的行數:

#!/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 ~
           
Shell--腳本統計檔案行數

二、擷取指定目錄特定擴充名檔案的行數

#!/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 ~
           

繼續閱讀