3

I am trying to parse some data from ifconfig output with sed, but I am not able to do it correctly. I want the command to extract just the number I am after.

For example, I am interested in extracting the bytes sent:

eth1      Link encap:Ethernet  HWaddr 00:00:00:09:15:f7  
      inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0
      inet6 addr: fe80::92e2:baff:fe08:35c7/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:75141 errors:0 dropped:0 overruns:0 frame:0
      TX packets:78046 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000 
      RX bytes:9040489 (9.0 MB)  TX bytes:34806464 (34.8 MB)

If I use sed:

ifconfig eth1 | sed 's|.*RX bytes:\([0-9]*\).*|\1|g'

I get this output:

eth1      Link encap:Ethernet  HWaddr 00:00:00:09:15:f7  
      inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0
      inet6 addr: fe80::92e2:baff:fe08:35c7/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:75141 errors:0 dropped:0 overruns:0 frame:0
      TX packets:78046 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000 
9040627

But I am only interested in '9040627' Is there a way to do it with sed, or should I use awk or other alternatives?

Edit: I am using busybox binaries, so my options are limited.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
fazineroso
  • 7,196
  • 7
  • 31
  • 42

3 Answers3

14

IMHO there is no standard for the ifconfig - output. It (may) change from system to system and from release to release.

If I were you, I would go for the /sys file system. You get all the information also from there - without the need of post-processing.

$ cat /sys/class/net/eth0/statistics/rx_bytes
37016050
Andreas Florath
  • 4,418
  • 22
  • 32
  • That would be the ideal answer, but the linux system I am working on is quite old and does not have /sys filesystem available. – fazineroso Apr 02 '12 at 14:27
  • Which Linux - System are you working on? Is `/proc/net/dev`available? – Andreas Florath Apr 02 '12 at 14:34
  • It's an embedded system with an old 2.6.x kernel. /proc/net/dev is available. I thought it would be easier to parse data from 'ifconfig' than from /proc/net/dev. – fazineroso Apr 03 '12 at 07:19
  • `These are used by the ifconfig(8) program to report device status.` man page for /proc/net/dev. IMHO the /proc interface is well defined. – Andreas Florath Apr 03 '12 at 07:39
10

use grep:

ifconfig | grep -oP '(?<=RX bytes:)[0-9]*'

use awk:

ifconfig | awk -F: '/RX bytes/{print $2+0}'
kev
  • 155,172
  • 47
  • 273
  • 272
  • Thanks. It works, but unfortunately I need to use it on a system with limited commands (busybox). any alternative? – fazineroso Apr 02 '12 at 13:08
  • 2
    You might have already found a good alternative, but here's mine (tested on OpenWRT's busybox): `awk '/eth0: / { print $2; }' /proc/net/dev` – Laszlo Valko Aug 11 '15 at 05:51
1

By default, sed prints out each line of the input, after any changes you've made to the line. Since you only want to print out something from the line with "RX bytes", you tell sed not to print every line (-n). So you want to specify the range on which the substitution should be performed, only the line that matches RX bytes, and then do the substitution and explicitly print the results.

ifconfig eth1 | sed '/RX bytes/{s|.*RX bytes:\([0-9]*\).*|\1|; p}'
jamessan
  • 41,569
  • 8
  • 85
  • 85