Score:0

Using awk to parse for conditions over multiple lines? (ifconfig output)

ir flag

I'm trying to parse ifconfig output with awk.

Right now my script is:

# Setup

ifconfig > ip.txt

ans=$(cat ip.txt | awk '$1 == "inet" {print $2}')

#output

echo "Here are your IPs:"

echo"$ans"

Output:

Here are your IPs:

192.168.1.1
192.168.2.1

What I want is an output more like

Here are your IPs: 

eth0 192.168.1.1
wlan0 192.168.2.1

But awk only parses by line, which is why I'm using inet as the $1 identifier for awk. As you know, ifconfig has multiple lines between the interface name and the IP address, so I can't increase that $2 to however many words away the address is.

I think the solution has to do with using RS or possibly using sed instead of awk. No luck using RS to change how awk sees lines.

Thanks in advance for any help.

muru avatar
us flag
But why `ifconfig`? It's deprecated and replaced by the `ip` tool, which conveniently has an option for outputting one line per interface just for this.
bee tea avatar
ir flag
True, but same issue for me: `6: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1420 qdisc mq state UP group default qlen 1000 link/ether 00:15:5d:ca:f4:0f brd ff:ff:ff:ff:ff:ff inet 172.18.236.6/20 brd 172.18.239.255 scope global eth0` Actual address is on a different line, so my current setup wouldn't be able to parse that info.
muru avatar
us flag
It has an **option** for one-line output: `ip -o addr` or `ip -oneline addr`
bee tea avatar
ir flag
@muru Thank you. When I change to using that, awk doesn't seem to like it.
muru avatar
us flag
What does that mean? Did they release a new version of awk with sentience?
bee tea avatar
ir flag
@muru haha, no, it means I am very bad at this and need to fiddle with it more. I appreciate your helping me out
muru avatar
us flag
You probably just need to check the fields you're using to compare and output
Score:1
lb flag
Vic

Well, if really wanna use ifconfig, you can do this:

# Setup
ifconfig > ip.txt

# Parse the output
ans=$(cat ip.txt | awk '/^[^ ]/{iface=$1} /inet / {print iface, $2}')

# Output
echo "Here are your IPs:"
echo "$ans"
Raffa avatar
jp flag
`cat` is not needed here ... i.e. `awk '...' ip.txt`
Vic avatar
lb flag
Vic
yeah, easier is just using ```ans=$(awk '/^[^ ]/{iface=$1} /inet / {print iface, $2}' ip.txt)```
Scott Gilbertson avatar
af flag
I'm new to this site, so my reputation won't let me comment, but it will let me answer... The answer Vic gate is good, but I just wanted to point out that you don't need the intermediate file ip.txt, because awk can read that text on stdin: echo "Here are your IPs:" ifconfig | awk '/^[^ ]/{iface=$1} /inet / {print iface, $2}'
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.