I'm trying to sum all bytes in "RX packets" lines of an ifconfig | grep "RX packets" output.
How do I do that?
Here's my code
#!/usr/lib/env bash
clear
result=0
while read i; do
line=${i##*'bytes'} | awk '{print $1;}'
(( "$result"+="$line" ))
echo "$result"
done <<< "$(ifconfig | grep "RX packets")"
Also: How should I extract those lines of bytes in a better way, doing "$(ifconfig | grep "RX packets")" and then line=${i##*'bytes'} | awk '{print $1;}' seems so ugly and complicated
if config | grep "RX packets" my output:
RX packets 7817232 bytes 9337993347 (9.3 GB)
RX packets 1240058 bytes 83114376 (83.1 MB)
RX packets 0 bytes 0 (0.0 B)
RX packets 188707 bytes 27682805 (27.6 MB)
Desired result - sum of all bytes:
9337993347 + 83114376 + 27682805
line=${i##*'bytes'} | awk '{print $1;}', what input do you expectawkto be seeing?ifconfigvaries by platform.ifconfig | grep "RX packets"line.awkscript, not a shell script that usesawkto do minimal processing on each line.