Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

From the command

watch -n1 "ifconfig eth0 | grep GiB"

I have given the output 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  
why can't you use it with Grep (grep takes regexes) – Frank Thomas May 21 '13 at 14:37
up vote 2 down vote accepted

Your test will break if the server reboots or is very busy: ifconfig scales the units it uses depending on how much data has been transferred. For example, a server I manage has this:

RX bytes:8697322454215 (7.9 TiB)  TX bytes:2151364718288 (1.9 TiB)

You might find it easier to watch the contents of /proc/net/dev, which is where ifconfig gets its data from. Its contents look like

Inter-|   Receive                                                |  Transmit
 face |bytes    packets errs drop fifo frame compressed multicast|bytes    packets errs drop fifo colls carrier compressed
  eth0:       0       0    0    0    0     0          0         0        0       0    0    0    0     0       0          0
    lo: 1204278   13020    0    0    0     0          0         0  1204278   13020    0    0    0     0       0          0
 wlan0: 658883131  568453    0    0    0     0          0         0 56092206  420036    0    0    0     0       0          0
share|improve this answer
    
nice suggestion do you know how i can read the results /proc/net/dev in real time? – asadz May 21 '13 at 14:52
1  
watch -n1 "cat /proc/net/dev" – Flup May 21 '13 at 14:53
1  
Well, that particular regex test will break, but it's still technically possible to use regex to parse ifconfig output. Simply use bytes: as the anchor string, which should be constant. – Bob May 21 '13 at 14:54
    
@Flup but the problem remains the same i just want last 5 numbers not the whole thing. – asadz May 21 '13 at 15:08

try sed:

| sed 's/.*(\([0-9.]*\s\)GiB)\s.*/\1/'
share|improve this answer
    
can you please break up the regex for my explaination? e.g what does /\1/ means? – asadz May 22 '13 at 4:34
    
\1 contains the number before GiB. – Endoro May 22 '13 at 5:01
    
it output me the whole ifconfig thing not just the tx , rx bytes – asadz May 22 '13 at 5:40
    
there are many various versions, I use the latest GNU. – Endoro May 22 '13 at 6:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .