bash - 没有临时文件的多个操作(使用自定义排除计算代码行)

时间:2012-07-16 07:22:11

标签: bash count

我希望通过散布的评论将每个操作保持在自己的行上

无论如何都可以在没有kludgy临时文件的情况下执行此操作

    #!/bin/sh

    git diff --stat `git hash-object -t tree /dev/null` > tmp.txt

    # not my code
    grep -v "^ kazmath" tmp.txt > tmp2.txt

    grep -v "\.obj " tmp2.txt > tmp.txt
    grep -v "\.png " tmp.txt > tmp2.txt
    grep -v "\.gbo " tmp2.txt > tmp.txt

    # not my code
    grep -v "obj2opengl\.pl " tmp.txt > tmp2.txt

    grep -v "\.txt " tmp2.txt > tmp.txt
    grep -v "\.md " tmp.txt > tmp2.txt
    grep -v "\.blend " tmp2.txt > tmp.txt

    # +'s at end of line
    sed 's/+*$//' tmp.txt > tmp2.txt

    # ditch last line
    sed '$d' < tmp2.txt > tmp.txt

    echo -n "lines of code "
    cut -d '|' -f 2 tmp.txt | awk '{ sum+=$1} END {print sum}'

    rm tmp.txt
    rm tmp2.txt

1 个答案:

答案 0 :(得分:1)

使用管道和更强大的正则表达式grep -E(又名egrep):

git diff --stat `git hash-object -t tree /dev/null` |
grep -v "^ kazmath" |
grep -E -v "\.(png|gbo|obj|txt|md|blend) " |
grep -v "obj2opengl\.pl " |
sed -e 's/+*$//' -e '$d' |
cut -d '|' -f 2 |
awk '{sum += $1 } END { print "lines of code " sum }'