Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.