Use find, wc, and sed to count lines

后端 未结 8 1482
攒了一身酷
攒了一身酷 2020-12-25 12:17

I was trying to use sed to count all the lines based on a particular extension.

find -name \'*.m\' -exec wc -l {} \\; | sed ...

I was tryi

相关标签:
8条回答
  • 2020-12-25 12:43

    For big directories we should use:

    find . -type f -name '*.m' -exec sed -n '$=' '{}' + 2>/dev/null | awk '{ total+=$1 }END{print total}' 
    
    # alternative using awk twice
    find . -type f -name '*.m' -exec awk 'END {print NR}' '{}' + 2>/dev/null | awk '{ total+=$1 }END{print total}' 
    
    0 讨论(0)
  • 2020-12-25 12:45

    sed is not the proper tool for counting. Use awk instead:

    find . -name '*.m' -exec awk '{print NR}' {} +
    

    Using + instead of \; forces find to call awk every N files found (like with xargs).

    0 讨论(0)
提交回复
热议问题