0

I have this output:

eth-g0    Link encap:Ethernet  HWaddr 44 
      inet addr:222.222.22.22  Bcafdst:0.0.0.0 
      UP BROADCAST RUNNING MULTICAST  MTU:1500 
      RX packets:9073618 errors:0 dropped:0 overruns:0 
      TX packets:6846448 errors:0 dropped:0 overruns:0 
      collisions:0 txqueuelen:1000 
      RX bytes:16fd45599237 (1.99 GiB)  TX bytes:1937392674 (1.8 
      Interrupt:36 
eth-g1    Link encap:Ethernet  HWaddr 44 
      inet addr:22.222.222.22  Bcast:0.2.2.2 
      UP BROADCAST MULTICAST  MTU:1500 
      RX packets:0 errors:0 dropped:0 overruns:0 
      TX packets:0 errors:0 dropped:0 overruns:0 
      collisions:0 txqueuelen:1200 
      RX bytes:0 (0.0 b)  TX bytes:0 (0.0 
      Interrupt:37 

I need this output:

eth-g0 inet 222.222.22.22

without the second line of the eth-g1 inet (ofcourse it is a fiktive ip)

I'm trying to output this in one line without \n

I have tried so far

ifconfig| egrep -o  "eth-g0...|inet................"

but it is not so good because some times there is

inet 222.22.22.222

and sometimes

inet addr 22.222.22.22

and it prints me with new lines everything.

Kusalananda
  • 333,661
  • You may find it easier to start with something easier to parse, for example the output of ip -brief -4 addr show eth-g0 – steeldriver Dec 20 '19 at 16:48
  • 2
    Do you want to https://unix.stackexchange.com/questions/8518/how-to-get-my-own-ip-address-and-save-it-to-a-variable-in-a-shell-script or https://unix.stackexchange.com/questions/426883/how-to-capture-the-first-ip-address-from-a-ifconfig-command/426890#426890 or https://unix.stackexchange.com/q/365225/117549 ? – Jeff Schaller Dec 20 '19 at 16:49
  • Note that there are three different ifconfig commands on Linux operating systems, and their outputs are not formatted in the same ways as one another. This question appears to be using the one from GNU inetutils, but does not actually say. https://unix.stackexchange.com/a/504084/5132 – JdeBP Dec 20 '19 at 18:19

2 Answers2

1
$ ifconfig | awk '/^eth-g0/ { iface = $1; getline; sub("addr:", ""); print iface, $1, $2 }'
eth-g0 inet 222.222.22.22

This parses the output using awk. When a line starting with the string eth-g0 is found, the first word is saved in a variable (this is the interface name), then the next line is immediately read and addr: is removed from the new line. Then the interface name together with the first two whitespace delimited words from the new line are printed.

Kusalananda
  • 333,661
  • Thank you very much for the answer! – Max Boyar Dec 20 '19 at 17:25
  • Note that on at least two of the ifconfigs one can restrict the interface and the address family somewhat by running ifconfig eth-g0 inet. Also note that, strictly speaking, there's no guarantee that the IPv4 address is on the second line. – JdeBP Dec 20 '19 at 18:33
  • @JdeBP I'm not aware of how ifconfig works on Linux. I'm just going by the data and description posted by the user. – Kusalananda Dec 20 '19 at 19:16
0

Your question is a little difficult to decipher, but I think this might be what you are trying to do:

ifconfig | grep -A1 "^eth-g0 " | grep -Eo "^eth[^ ]+|inet (addr )?[^ ]+" | paste - -

I've based this solution on the commands you are trying to run, in the hope that it's easier to understand.

It's probably not the optimal way to do this.

bxm
  • 4,855