linux shell 统计行数的8种方法

方法一
awk '{print NR}' test1.sh|tail -n1
示例:

[root@adiislogdata164 test]# ls
front_action.log.2015-05-13-00.log
[root@adiislogdata164 test]# awk '{print NR}' front_action.log.2015-05-13-00.log | tail -n1
273736
[root@adiislogdata164 test]#

方法二
awk 'END{print NR}' test1.sh

示例:

[root@adiislogdata164 test]# ls
front_action.log.2015-05-13-00.log
[root@adiislogdata164 test]# awk 'END{print NR}' front_action.log.2015-05-13-00.log 
273736
[root@adiislogdata164 test]#

方法三
grep -n "" test1.sh|awk -F: '{print '}|tail -n1
示例:

[root@adiislogdata164 test]# grep -n "" front_action.log.2015-05-13-00.log|awk -F: '{print '}|tail -n1
273736:2015-05-13 00:59:59|@183.31.247.46|@15b55bbd-df33-4a64-9838-44ffd34a23f3|@176|@1|@44|@58|@1-39|@cn020_0756_[gd_zh]|@ent.ifeng.com|@0|@0
[root@adiislogdata164 test]#

方法四
sed -n '$=' test1.sh
示例:

[root@adiislogdata164 test]# ls
front_action.log.2015-05-13-00.log
[root@adiislogdata164 test]# sed -n '$=' front_action.log.2015-05-13-00.log 
273736
[root@adiislogdata164 test]#

方法五
wc -l test1.sh
示例:

[root@adiislogdata164 test]# ls
front_action.log.2015-05-13-00.log
[root@adiislogdata164 test]# wc -l front_action.log.2015-05-13-00.log
273736 front_action.log.2015-05-13-00.log
[root@adiislogdata164 test]#

方法六
cat test1.sh |wc -l

示例:

[root@adiislogdata164 test]# ls
front_action.log.2015-05-13-00.log
[root@adiislogdata164 test]# cat front_action.log.2015-05-13-00.log | wc -l
273736
[root@adiislogdata164 test]#

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

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

示例:
linux shell 统计行数的8种方法_第1张图片

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

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

示例:
linux shell 统计行数的8种方法_第2张图片

 

 

你可能感兴趣的:(linux shell 统计行数的8种方法)