Shell脚本统计文件行数

时间:2021-07-30 07:20:57

Shell脚本统计文件行数

转自 http://www.jb51.net/article/61943.htm 
 
示例: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=
linesCount=
function funCount()
{
for file in ` ls $ `
do
if [ -d $"/"$file ];then
funCount $"/"$file
else
declare -i fileLines
fileLines=`sed -n '$=' $"/"$file`
let linesCount=$linesCount+$fileLines
let filesCount=$filesCount+
fi
done
} if [ $# -gt ];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=
linesCount=
function funCount()
{
for file in ` ls $ `
do
if [ -d $"/"$file ];then
funCount $"/"$file
else
fileName=$"/"$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+
fi
fi
done
} if [ $# -gt ];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 ~