shell统计行数

32F  2018/11/22 linux



查看单个文件行数

新建test.sh文件
方法1:awk '{print NR}' test.sh|tail -n1
方法2:awk 'END{print NR}' test.sh
方法3:sed -n '$=' test1.sh

查看多个文件总行数

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

使用: ./count.sh 统计当前目录 ./count.sh /tmp ~统计多个目录


添加评论
全部回复
暂无回复