问题
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 awk
wardly 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