AWK和功率问题(DD-WRT)

时间:2021-04-02 16:58:06

I'm trying to extract the sum of numbers to a variable (cwr) in a file (wa_cidr) using this command:

我正在尝试使用此命令将数字总和提取到文件(wa_cidr)中的变量(cwr):

cwr=$(grep -E "^([0-9]{1,3}\.){3}[0-9]{1,3}(\/([0-9]|[1-2][0-9]|3[0-2]))?$" /tmp/wa_cidr | awk -F '/' '`{n += 2**(32 - $NF)}` END {print n}')

However, the output is empty.

但是,输出为空。

If I change the part...

如果我改变了部分......

{n += 2**(32 - $NF)} 

To...

至...

{n += (32 - $NF)} 

I get a valid result written to the cwr variable.

我得到一个写入cwr变量的有效结果。

It would seem that I cannot do the power of (32 - $NF) using either 2**X or 2^X in AWK.

似乎我无法在AWK中使用2 ** X或2 ^ X来执行(32 - $ NF)的功能。

If I do it on the command line, e.g. using...

如果我在命令行上执行此操作,例如使用...

$ echo $(2**5)

There's no problem and the result is 32.

没有问题,结果是32。

I have tried many variations on the formula (parantheses etc.) but nothing seems to Work.

我已经尝试了很多公式(parantheses等)的变化,但似乎没有任何工作。

What is wrong? Can it be done in another way?

哪里不对?可以用另一种方式完成吗?

Thanks, Søren

谢谢,Søren

2 个解决方案

#1


1  

If your regexp is correct then this would be the correct syntax to do what you appear to be trying to do:

如果你的正则表达式是正确的,那么这将是你正在尝试做的事情的正确语法:

cwr=$(awk -F'/' '/^([0-9]{1,3}\.){3}[0-9]{1,3}(\/([0-9]|[1-2][0-9]|3[0-2]))?$/{n += 2^(32 - $NF)} END {print n+0}' /tmp/wa_cidr)

#2


1  

Hmmm, it would appear that I was on to something, when I mentioned that something was limited in the DD-WRT Shell.

嗯,当我提到DD-WRT Shell中的某些内容有限时,看起来我还有所作为。

According to this website, https://rosettacode.org/wiki/Exponentiation_operator#awk, the "traditional awk implementations do not provide an exponent operator, so we define a function to calculate the exponent".

根据这个网站,https://rosettacode.org/wiki/Exponentiation_operator#awk,“传统的awk实现不提供指数运算符,所以我们定义了一个函数来计算指数”。

So for the code to Work it needs to look something like this:

因此,要使代码工作,它需要看起来像这样:

cwr=$(grep -E "^([0-9]{1,3}\.){3}[0-9]{1,3}(\/([0-9]|[1-2][0-9]|3[0-2]))?$" /tmp/wa_cidr | awk -F '/' 'function pow(x,n){r=1;for(i=0;i<n;i++)r=r*x;return r}{c += pow(2,(32-$NF))} END {print c}')

Which gives the correct value of 3996.

其中给出了正确的3996值。

Thank you all for inputs!

谢谢大家的投入!

/Søren

/索伦

#1


1  

If your regexp is correct then this would be the correct syntax to do what you appear to be trying to do:

如果你的正则表达式是正确的,那么这将是你正在尝试做的事情的正确语法:

cwr=$(awk -F'/' '/^([0-9]{1,3}\.){3}[0-9]{1,3}(\/([0-9]|[1-2][0-9]|3[0-2]))?$/{n += 2^(32 - $NF)} END {print n+0}' /tmp/wa_cidr)

#2


1  

Hmmm, it would appear that I was on to something, when I mentioned that something was limited in the DD-WRT Shell.

嗯,当我提到DD-WRT Shell中的某些内容有限时,看起来我还有所作为。

According to this website, https://rosettacode.org/wiki/Exponentiation_operator#awk, the "traditional awk implementations do not provide an exponent operator, so we define a function to calculate the exponent".

根据这个网站,https://rosettacode.org/wiki/Exponentiation_operator#awk,“传统的awk实现不提供指数运算符,所以我们定义了一个函数来计算指数”。

So for the code to Work it needs to look something like this:

因此,要使代码工作,它需要看起来像这样:

cwr=$(grep -E "^([0-9]{1,3}\.){3}[0-9]{1,3}(\/([0-9]|[1-2][0-9]|3[0-2]))?$" /tmp/wa_cidr | awk -F '/' 'function pow(x,n){r=1;for(i=0;i<n;i++)r=r*x;return r}{c += pow(2,(32-$NF))} END {print c}')

Which gives the correct value of 3996.

其中给出了正确的3996值。

Thank you all for inputs!

谢谢大家的投入!

/Søren

/索伦