使用管道划分bash的最佳方法? [英] Best way to divide in bash using pipes?

查看:73
本文介绍了使用管道划分bash的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在寻找一种简单的除数方法(或提供其他数学函数).假设我有以下命令:

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

如何获取wc -l的结果并将其除以3?

How can I take the result of wc -l and divide it by 3?

我所看到的示例没有处理重定向/导入.

The examples I've seen don't deal with re-directed out/in.

推荐答案

使用bc:

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

相反,bash shell仅执行整数运算.

In contrast, the bash shell only performs integer arithmetic.

Awk也非常强大:

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

如果使用awk,甚至不需要wc:

You don't even need wc if using awk:

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

这篇关于使用管道划分bash的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆