gpt4 book ai didi

linux - awk 错误 : each rule must have a pattern or an action part

转载 作者:太空宇宙 更新时间:2023-11-04 10:51:20 25 4
gpt4 key购买 nike

来自 here我得到以下命令:

awk '/em1/ {i++; rx[i]=$2; tx[i]=$10}; END{print rx[2]-rx[1] " " tx[2]-tx[1]}' \
<(cat /proc/net/dev; sleep 1; cat /proc/net/dev)

它完全按预期工作。但是,我会以 Mbps 为单位输出,所以我创建了 2 个命令,一个用于上传,一个用于下载(均有效):

awk '/^e/ {i++; rx[i]=$2}; END{printf("%.2f Mbps", \
((rx[2]-rx[1])/1024/1024))}' <(cat /proc/net/dev; sleep 1; \
cat /proc/net/dev) # download

awk '/^e/ {i++; tx[i]=$10}; END{printf("%.2f Mbps", \
((tx[2]-tx[1])/1024/1024))}' <(cat /proc/net/dev; sleep 1; \
cat /proc/net/dev) # upload

但是当我尝试合并时,出现了一些错误:

$ awk '/^e/ {i++; rx[i]=$2}; tx[i]=$10}; \
END{printf(" down: %.2f Mbps, up: %.2f Mbps", \
((rx[2]-rx[1])/1024/1024)), ((tx[2]-tx[1])/1024/1024))}' \
<(cat /proc/net/dev; sleep 1; cat /proc/net/dev)

awk: cmd. line:1: /^e/ {i++; rx[i]=$2}; tx[i]=$10}; END{printf(" down: %.2f Mbps, up: %.2f Mbps", ((rx[2]-rx[1])/1024/1024)), ((tx[2]-tx[1])/1024/1024))}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: each rule must have a pattern or an action part
awk: cmd. line:1: /^e/ {i++; rx[i]=$2}; tx[i]=$10}; END{printf(" down: %.2f Mbps, up: %.2f Mbps", ((rx[2]-rx[1])/1024/1024)), ((tx[2]-tx[1])/1024/1024))}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: /^e/ {i++; rx[i]=$2}; tx[i]=$10}; END{printf(" down: %.2f Mbps, up: %.2f Mbps", ((rx[2]-rx[1])/1024/1024)), ((tx[2]-tx[1])/1024/1024))}
awk: cmd. line:1: ^ syntax error

我尝试用sprintf解决,结果还是一样。

操作系统:Linux 4.0.5-1-ARCH x86_64 GNU/Linux

awk:GNU Awk 4.1.3,API:1.1(GNU MPFR 3.1.2-p11,GNU MP 6.0.0)

最佳答案

错误,正如 awk 有用地(虽然冗长)告诉你的那样

awk: cmd. line:1: /^e/ {i++; rx[i]=$2}; tx[i]=$10}; END{printf(" down: %.2f Mbps, up: %.2f Mbps", ((rx[2]-rx[1])/1024/1024)), ((tx[2]-tx[1])/1024/1024))}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: each rule must have a pattern or an action part
awk: cmd. line:1: /^e/ {i++; rx[i]=$2}; tx[i]=$10}; END{printf(" down: %.2f Mbps, up: %.2f Mbps", ((rx[2]-rx[1])/1024/1024)), ((tx[2]-tx[1])/1024/1024))}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: /^e/ {i++; rx[i]=$2}; tx[i]=$10}; END{printf(" down: %.2f Mbps, up: %.2f Mbps", ((rx[2]-rx[1])/1024/1024)), ((tx[2]-tx[1])/1024/1024))}
awk: cmd. line:1: ^ syntax error

是关闭 因为你已经在 rx[i]=$2}; 之前关闭了那个 Action block 和 , 和最后的 ) 因为 printf 调用已经关闭。完整的函数调用是这样的:

printf(" down: %.2f Mbps, up: %.2f Mbps", ((rx[2]-rx[1])/1024/1024))

因此 及其后的所有内容都是语法错误,因为它本身不是有效的语句。

关于linux - awk 错误 : each rule must have a pattern or an action part,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30904208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com