2

I'm using ifconfig on OpenSUSE. When I run ifconfig eth0 I get

eth0  Link encap:Ethernet  HWaddr CE:FD:75:DF:A5:6D
      inet addr:172.16.4.177  Bcast:172.16.5.255  Mask:255.255.254.0
      inet6 addr: fe80::adfd:75ef:fedf:v56d/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
      RX packets:11812456 errors:0 dropped:2 overruns:0 frame:0
      TX packets:7000495 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000
      RX bytes:2591436376 (2471.3 Mb)  TX bytes:9196901478 (8770.8 Mb)

I'm looking to format this so each parameter gets returned on a new line using sed or awk e.g.:

   eth0  
         Link encap:Ethernet
         HWaddr CE:FD:75:DF:A5:6D
         inet addr:172.16.4.177  
         Bcast:172.16.5.255  
         Mask:255.255.254.0
         inet6 addr: fe80::adfd:75ef:fedf:v56d/64 
         Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  
         Metric:1
         RX packets:11812456 
         errors:0 
         dropped:2
         overruns:0 
         frame:0
         TX packets:7000495 
         errors:0
         dropped:0 
         overruns:0 
         carrier:0
         collisions:0
         txqueuelen:1000
         RX bytes:2591436376 (2471.3 Mb)
         TX bytes:9196901478 (8770.8 Mb)

I've tried ifconfig eth0 | sed 's/ /\r/' but that doesn't seem to split on the double space.

  • sed 's/ \+/\n\t/g' is not satisfy? – Costas Jan 21 '15 at 12:06
  • 1
    @Costas :Yeah it will do but if u look at OP's question carefully he wants key value pair in a single line that is Link encap:Ethernet – Thushi Jan 21 '15 at 12:09
  • @Thushi Sorry, server is strip second space in script while copying: sed 's/ \+/\n\t/g' – Costas Jan 21 '15 at 12:47
  • Note that \r? is just a carriage return. It won't throw a newline, it'll just send the cursor back to the start of the line. That can be handy though, eg for((i=0;i<=250;i+=5));do printf "\r%3d " "$i";sleep .1;done;echo – PM 2Ring Jan 21 '15 at 13:05
  • ifconfig is deprecated - it hasn't seen a linux release in more than a decade. Stop using it. Use ip. – mikeserv Jan 21 '15 at 16:36
1

You can start with

sed 's/\(:[^: ]\+\) \([^(]\)/\1\n\2/g;s/\()\)/\1\n/;s/^ \+//'

it should be close enough, and most probably can be simplified and optimized further. The result:

eth0  Link encap:Ethernet
 HWaddr CE:FD:75:DF:A5:6D
inet addr:172.16.4.177
 Bcast:172.16.5.255
 Mask:255.255.254.0
inet6 addr: fe80::adfd:75ef:fedf:v56d/64
Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500
 Metric:1
RX packets:11812456
errors:0
dropped:2
overruns:0
frame:0
TX packets:7000495
errors:0
dropped:0
overruns:0
carrier:0
collisions:0
txqueuelen:1000
RX bytes:2591436376 (2471.3 Mb)
  TX bytes:9196901478 (8770.8 Mb)
0

The following script used the intermediate substitution to = symbol (you are free to change to any you'l want):

#!/bin/sed -f
s/\s\+\s/=/g
/:[^=:]\+:/s/:[^=:]\+ /&=/g
s/^=/\t/
s/ *=$//
s/ *=/\n\t/g

So

ifconfig eth0 | sed -f script.above

outputs:

eth0
    Link encap:Ethernet
    HWaddr 74:2f:68:8b:b8:6e
    inet addr:192.168.1.247
    Bcast:192.168.1.255
    Mask:255.255.255.224
    inet6 addr: fe80::762f:68ff:fe8b:b86e/64
    Scope:Link
    UP BROADCAST RUNNING MULTICAST
    MTU:1500
    Metric:1
    RX packets:3128543
    errors:0
    dropped:1
    overruns:0
    frame:0
    TX packets:1346967
    errors:0
    dropped:0
    overruns:0
    carrier:0
    collisions:0
    txqueuelen:1000
    RX bytes:4538616348 (4.5 GB)
    TX bytes:118149772 (118.1 MB)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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