简体   繁体   English

Bash Awk 命令混淆

[英]Bash Awk command confusion

I was checking a piece of code and I can't fully understand it:我正在检查一段代码,但我无法完全理解它:

var1=`awk -F , '{printf "%.0f\n", $1*17/1000}' $somefile`;

Let's say that the number it receives from the file is 1000, it multiplies it by 17 and divides by 1000, but before that, there's this: "%.0f\\n" I know how rest of the calculations go, but that part confuses me.假设它从文件中接收到的数字是 1000,它将它乘以 17 再除以 1000,但在此之前,还有这个: "%.0f\\n"我知道其余的计算是如何进行的,但这部分令人困惑我。 As far as I understand it, it's calculating a desimal of something and prints a newline.据我了解,它正在计算某事物的一个小数并打印一个换行符。 An answer with a pit of explanation of the awk command in whole would be appreciated :D一个完整的 awk 命令解释坑的答案将不胜感激:D

It sets the number of decimal points to zero (truncates the number) after the computation.它在计算后将小数点数设置为零(截断数字)。 You can find more information here你可以在这里找到更多信息

You can try it out yourself by typing您可以通过键入自己尝试

awk -F, '{printf "%.0f\n", $1*17/1000}'

on the command line, it will then convert your input for each entered line, but perhaps this will be more intuitive.在命令行上,它将为每个输入的行转换您的输入,但也许这会更直观。 (Control-D to exit) (Control-D 退出)

awk -F, '{printf "%.0f\n", $1}'

ps.附: Note that this is equivalent to integer formatting %d or %i (no decimal points)请注意,这等效于整数格式 %d 或 %i(无小数点)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM