bash 中的 awk 和 printf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1505063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
awk and printf in bash
提问by Tim
I am trying to get the rounded number of the average load in the past 5 mins. So here goes my command:
我试图获得过去 5 分钟内平均负载的四舍五入数。所以这是我的命令:
uptime | awk -F, '{print }'|printf "%.0f\n"
It seems incorrect as it always give me 0.
这似乎不正确,因为它总是给我 0。
If I tried to use a variable as intermediate between awk and printf, then it is correct
如果我尝试使用一个变量作为 awk 和 printf 之间的中间变量,那么它是正确的
avgload=$(uptime | awk -F, '{print }')
printf "%.0f\n" $avgload
So anything wrong with my first try?
那么我的第一次尝试有什么问题吗?
Thanks and regards!
感谢致敬!
UPDATE:
更新:
Just for getting the average load in the past 5 mins, here is the output of uptime on my linux server (Kubuntu)
只是为了获得过去 5 分钟的平均负载,这是我的 linux 服务器(Kubuntu)上正常运行时间的输出
$ uptime
13:52:19 up 29 days, 18 min, 15 users, load average: 10.02, 10.04, 9.58
$ uptime
13:52:19 up 29 days, 18 min, 15 users, load average: 10.02, 10.04, 9.58
On my laptop (Ubuntu) it is similar
在我的笔记本电脑(Ubuntu)上它是相似的
`$ uptime
`$ 正常运行时间
13:53:58 up 3 days, 12:02, 8 users, load average: 0.29, 0.48, 0.60 `
13:53:58 up 3天,12:02,8个用户,平均负载:0.29, 0.48, 0.60 `
That's why I take the 5th field.
这就是我选择第 5 个字段的原因。
回答by Paul
The 5th comma-separated field from the uptime output is non-existant (on my system at least), which is why you keep getting zero. The 5-minute uptime is the second-to-last field, so this works:
正常运行时间输出中的第 5 个逗号分隔字段不存在(至少在我的系统上),这就是为什么你一直得到零。5 分钟的正常运行时间是倒数第二个字段,所以这有效:
uptime | awk '{printf "%.0f\n",$(NF-1)}'
回答by Will Bickford
Simplest version (use built-in awk printf - kudos Dennis Williamson):
最简单的版本(使用内置的 awk printf - 荣誉丹尼斯威廉姆森):
uptime |awk -F, '{printf "%0.f\n",}'
Original answer: Run it through xargs instead.
原始答案:改为通过 xargs 运行它。
uptime |awk -F, '{print }' |xargs printf "%0.f\n"
回答by marco
If you have /proc mounted you can use /proc/loadavg:
如果您安装了 /proc,您可以使用 /proc/loadavg:
LANG=C
read _ uptime _ </proc/loadavg
printf "%.0f\n" $uptime
The above code uses only built-in shell commands. However, if you like, you can use awk too:
上面的代码只使用了内置的 shell 命令。但是,如果您愿意,也可以使用 awk:
awk '{printf "%.0f\n",}' /proc/loadavg
回答by Mark Rushakoff
You can just do
你可以做
printf "%.0f\n" `uptime | awk -F, '{print }'`
The backticks are essentially command substitution, so whatever the output of uptime | awk -F, '{print $5}'is, will be the argument to printf.
反引号本质上是命令替换,因此无论输出uptime | awk -F, '{print $5}'是什么,都将是 的参数printf。
The problem with the first approach is that printf just does not accept arguments from stdin; if it didtake arguments from stdin, then it would have worked fine. Also, no arguments to printf apparently does mean "put zero in for my arguments".
第一种方法的问题在于 printf 不接受来自stdin; 的参数。如果它确实从 中获取参数stdin,那么它会工作得很好。此外, printf 没有参数显然意味着“为我的参数输入零”。
$ echo hello | printf "%s"
$ printf "%s" hello
hello
$ printf "%.0f"
0
回答by Jeremy Bourque
I don't think you can feed the input to printf on stdin. Try this version:
我认为您不能在 stdin 上将输入提供给 printf。试试这个版本:
printf "%.0f\n" `uptime | awk -F, '{print }'`

