当前位置:   article > 正文

linux shell 统计行数的8种方法_shell中计算行数

shell中计算行数

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

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

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

示例:

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

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

  1. [root@adiislogdata164 test]# grep -n "" front_action.log.2015-05-13-00.log|awk -F: '{print '}|tail -n1
  2. 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
  3. [root@adiislogdata164 test]#

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

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

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

  1. [root@adiislogdata164 test]# ls
  2. front_action.log.2015-05-13-00.log
  3. [root@adiislogdata164 test]# wc -l front_action.log.2015-05-13-00.log
  4. 273736 front_action.log.2015-05-13-00.log
  5. [root@adiislogdata164 test]#

方法六
cat test1.sh |wc -l

示例:

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

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

  1. #!/bin/bash
  2. filesCount=0
  3. linesCount=0
  4. function funCount()
  5. {
  6. for file in ` ls $1 `
  7. do
  8. if [ -d $1"/"$file ];then
  9. funCount $1"/"$file
  10. else
  11. declare -i fileLines
  12. fileLines=`sed -n '$=' $1"/"$file`
  13. let linesCount=$linesCount+$fileLines
  14. let filesCount=$filesCount+1
  15. fi
  16. done
  17. }
  18. if [ $# -gt 0 ];then
  19. for m_dir in $@
  20. do
  21. funCount $m_dir
  22. done
  23. else
  24. funCount "."
  25. fi
  26. echo "filesCount = $filesCount"
  27. echo "linesCount = $linesCount"

使用方法:
1)针对本目录
./count.sh

2)统计多个目录
./count.sh /tmp ~

示例:

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

  1. #!/bin/bash
  2. extens=(".c" ".cpp" ".h" ".hpp")
  3. filesCount=0
  4. linesCount=0
  5. function funCount()
  6. {
  7. for file in ` ls $1 `
  8. do
  9. if [ -d $1"/"$file ];then
  10. funCount $1"/"$file
  11. else
  12. fileName=$1"/"$file
  13. EXTENSION="."${fileName##*.}
  14. echo "fileName = $fileName Extension = $EXTENSION"
  15. if [[ "${extens[@]/$EXTENSION/}" != "${extens[@]}" ]];then
  16. declare -i fileLines
  17. fileLines=`sed -n '$=' $fileName`
  18. echo $fileName" : "$fileLines
  19. let linesCount=$linesCount+$fileLines
  20. let filesCount=$filesCount+1
  21. fi
  22. fi
  23. done
  24. }
  25. if [ $# -gt 0 ];then
  26. for m_dir in $@
  27. do
  28. funCount $m_dir
  29. done
  30. else
  31. funCount "."
  32. fi
  33. echo "filesCount = $filesCount"
  34. echo "linesCount = $linesCount"

使用方法:
1、针对本目录
./count.sh

2、统计多个目录
./count.sh /tmp ~

示例:


 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/578292
推荐阅读
相关标签
  

闽ICP备14008679号