awk error: each rule must have a pattern or an action part

awk error: each rule must have a pattern or an action part

here 我得到以下命令:

awk '/em1/ {i++; rx[i]=; tx[i]=}; 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]=}; 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]=}; 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]=}; tx[i]=}; \
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]=}; tx[i]=}; 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]=}; tx[i]=}; 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]=}; tx[i]=}; 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来解决,结果还是一样

OS: 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]=}; tx[i]=}; 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]=}; tx[i]=}; 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]=}; tx[i]=}; 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]=}; 之前关闭了那个动作块, , 和最后的 ) 因为 printf电话已经关闭。完整的函数调用是这样的:

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

所以 , 和它后面的所有内容都是语法错误,因为它本身不是一个有效的语句。