awk错误:每个规则必须有一个模式或一个动作部分

时间:2015-06-17 23:35:39

标签: linux gawk

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)

1 个答案:

答案 0 :(得分:1)

错误,因为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};之前和,以及最后)关闭了该操作阻止,因为printf来电是printf(" down: %.2f Mbps, up: %.2f Mbps", ((rx[2]-rx[1])/1024/1024)) 已经关闭。完整的函数调用是:

,

因此int j= 1; for(int i=0; i< 5; i++) { while(i <3) { j++; } } 及其后面的所有内容都是语法错误,因为它本身并不是一个有效的语句。