awk syntax errors with double star **

烂漫一生 提交于 2021-02-10 14:59:45

问题


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 {

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

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


回答1:


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

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.



来源:https://stackoverflow.com/questions/9913368/awk-syntax-errors-with-double-star

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!