带有双星的awk语法错误** [英] awk syntax errors with double star **

查看:88
本文介绍了带有双星的awk语法错误**的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Debian 6和GNU sed,试图让awk将 du 的输出从一长串字节转换为后缀如Mb和Kb的更易理解的数字.(我知道您可以使用 -h 选项,但我想使用awk手动执行此操作.)

I'm using Debian 6 and GNU sed, trying to get awk to convert the output of du from a long string of bytes to a more human readable number with suffixes like Mb and Kb. (I know you can use the -h option, but I want to do this manually with awk.)

到目前为止,我的命令如下所示(我添加了换行符以使其更具可读性):

So far, my command looks like this (I put in the newlines to make it more readable):

du /test.img | grep [0-9]* | awk "{ sum=$1 ; hum[1024**3]='Gb';hum[1024**2]='Mb';
hum[1024]='Kb'; for (x=1024**3; x>=1024; x/=1024){ if (sum>=x)
{ printf '%.2f %s\n',sum/x,hum[x];break } }}"

但是,我从双星附近获得语法错误:

However, I get syntax errors from/near the double stars:

awk: line 1: syntax error at or near *
awk: line 1: syntax error at or near *
awk: line 1: syntax error at or near *
awk: line 1: extra ')'
awk: line 1: syntax error at or near {

如果我将所有双星都设为 ** ,而只有一颗星为 * ,则awk会运行,但是我得到的数字不正确.

If I make all double stars ** just one star *, awk runs, but I get an incorrect number.

我该如何解决这些语法错误,而不再被 awk 所迷惑? ba dum tshh

What can I do to fix these syntax errors and stop being so awkwardly confused? ba dum tshh

推荐答案

只是使其成为官方"(作为答案)...

Just to make it "official" (as an answer)...

您正在使用 ** 求幂.在 awk 中, ^ 运算符用于求幂.我想互联网上有很多参考,但是我发现的是此处.

You're using ** for exponentiation. In awk, the ^ operator is used for exponentiation. I imagine there are many references on the internet, but the one I found is here.

这篇关于带有双星的awk语法错误**的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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