1

From the command

watch -n1 "ifconfig eth0 | grep GiB". I have output given below,

Now i just want the last 5 digits before (GiB). I wrote an regex for it, and it seems to work

(\d{1,4})(?!.*\d)\sGiB

The only problem is that i cannot use it with the command i showed above? I'm using ubuntu any help

enter image description here

asadz
  • 167

1 Answers1

3

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.

Oli
  • 293,335
  • Thanks for the suggestion, I'm using watch for one time basis, I'm considering using iptraf in future i ran your command but i don't think its giving me any changes in real time basis? – asadz May 21 '13 at 15:01
  • Yeah, it'll update every two seconds. Obviously, once you're up to GiB, you need to shift a fair amount of traffic before it'll change. – Oli May 21 '13 at 15:10
  • I'm not sure what does sh -c does, seems like a counter, the difference i'm getting this command is that i cannot see per sec changes to the output like i can do in the original command. The traffic I'm getting is about 30 Mb/s its span port traffic. – asadz May 21 '13 at 15:19
  • 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 of ifconfig through grep. 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:31
  • (And this is tested. It works. I've just sat and watched the numbers while around while I did a test with iperf) – Oli May 21 '13 at 15:34
  • for me adding -x switch gives me syntax error ; i think that switch is not supported in my version of watch. I think i was not be able to explain my position in form of live example. This is how i want my screen to look after the command runs. http://www.rubular.com/r/xaf0yx7hTZ – asadz May 21 '13 at 17:20