From here I got the following command:
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)
which is fully working as intended. However, I would the output in Mbps, so I created 2 commands, one for upload, one for download (both working):
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
But when I tried to combine then, but some errors came up:
$ 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
I tried to solve it using sprintf
, but the result was the same.
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)
The errors, as awk helpfully (though verbosely) tells you
are that closing
}
because you already closed that action block atrx[i]=$2};
before that and the,
and the final)
because theprintf
call was already closed. The complete function call is this:So the
,
and everything that follows it is a syntax error because it isn't a valid statement on its own.