0

ifconfig output:

lo: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 33192
        inet 127.0.0.1 netmask 0xff000000
        inet6 ::1 prefixlen 128
        inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
eth0: flags=8b43<UP,BROADCAST,RUNNING,PROMISC,ALLMULTI,SIMPLEX,MULTICAST> mtu 1500
        address: 01:02:03:04:05:06
        media: Ethernet 1000baseT full-duplex
        status: active
        inet 192.168.0.10 netmask 0xffff0000 broadcast 192.254.255.255
        inet alias 0.0.0.0 netmask 0xff000000 broadcast 255.255.255.255
        inet6 fe80::0:0:0:01%eth0 prefixlen 64 scopeid 0x4
vlan01: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
        vlan: 01 priority: 0 parent: eth0
        address: 01:02:03:04:05:06
        inet 192.168.0.11 netmask 0xfffffff0 broadcast 192.254.255.255
        inet6 fe80::0:0:0:02%vlan01 prefixlen 64 scopeid 0x6
        inet6 2a03:0:0:0::e1 prefixlen 64

Note, that for vlan01 there is a record 'parent: eth0'. What I need is to get vlan01 for this particular output. I have only sed and grep at my disposal.

Is it possible with ifconfig -a | sed '...'?

2 Answers 2

0

We can do that with sed:

#!/bin/sed -nf

# If it begins with anything except whitespace, trim it down to the
# bit before ":", and store that into hold space.
/^[^ ]/{
s/:.*//
h
}

# If we see "parent: eth0", then print the hold space.
/parent: eth0/{
g
p
}

With your input, this outputs vlan01 (and a newline).

0

Using only grep:

ifconfig | grep -B1 parent | grep -oh ^[a-z0-9]*
# -B num - Print num lines of leading context before matching lines.
# -o Print only the matched (non-empty) parts of matching lines, with each such part on a separate output line.

This outputs vlan01 for the output provided in your question. Note that my example only looks for parent. The pattern should be updated to reflect what you want - parent: eth0or other.

You must log in to answer this question.

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