zoukankan      html  css  js  c++  java
  • Shell脚本统计文件行数

    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

    一、获取指定目录所有文件的行数:

    #!/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 ~
  • 相关阅读:
    最新 Cocos2d-x 3.2 开发环境搭建(windows环境下)
    Apache OFbiz entity engine源代码解读
    Android 标签控件
    标准红外遥控的接收程序-松瀚汇编源程序
    uva 10548
    char* 和char[]的差别
    依据波形的转折点文件,转换成波形文件
    spring 中StoredProcedure的使用方法
    JMS的样例
    JavaScript高级编程
  • 原文地址:https://www.cnblogs.com/fieldtianye/p/10728549.html
Copyright © 2011-2022 走看看