Best way to divide in bash using pipes?

后端 未结 4 1616

I\'m just looking for an easy way to divide a number (or provide other math functions). Let\'s say I have the following command:

find . -name \'*.mp4\' | wc -l
<         


        
4条回答
  •  太阳男子
    2021-02-02 14:33

    Using bc:

    $ bc -l <<< "scale=2;$(find . -name '*.mp4' | wc -l)/3"
    2.33
    

    In contrast, the bash shell only performs integer arithmetic.

    Awk is also very powerful:

    $ find . -name '*.mp4' | wc -l | awk '{print $1/3}'
    2.33333
    

    You don't even need wc if using awk:

    $ find . -name '*.mp4' | awk 'END {print NR/3}'
    2.33333
    

提交回复
热议问题