You wouldn't parse the watch command, you'd parse whatever command watch was running. I pimped your search to match a wider range of possible output from ifconfig:
$ ifconfig eth0 | grep -Eo '[0-9\.]+ [PTGMK]i?B'
164.8 GB
142.6 GB
This is made possible by grep's -E
argument which allows a wider syntax and -o
which only outputs matching strings.
If you want to loop that, you can but you have to wrap it in a shell so the pipe is interpreted correctly:
watch -x sh -c "ifconfig eth0 | grep -Eo '[0-9\.]+ [PTGMK]i?B'"
But in my opinion, this isn't wildly useful as it is... watch
is really only good for a real person watching the screen. If that's the case, you're all done but if you want to do something with these numbers on a regular basis, you're probably using the wrong tool.
sh -c "..."
is a way of running multiple commands that would otherwise be interpreted outside of that shell, together. Here it allows me to pipe the internal output ofifconfig
throughgrep
. The sh command is being run every 2 seconds (watch
's default) and in turn, sh runs the command we give it. If you want it to be every second, stick a-n1
in after the-x
. – Oli May 21 '13 at 15:31iperf
) – Oli May 21 '13 at 15:34