Extracting IP address from a line from ifconfig output with grep

Viewed 79395

Given this specific line pulled from ifconfig, in my case:

inet 192.168.2.13 netmask 0xffffff00 broadcast 192.168.2.255

How could one extract the 192.168.2.13 part (the local IP address), presumably with regex?

12 Answers

This code works for me on raspberry pi zero w.

(extract wlan0: inet 192.168.x.y address from ifconfig output)

Search for pattern 'inet 192' in ifconfig output and get the 10th position using space delimiter.

 $> ifconfig |grep 'inet 192'|cut -d' ' -f10

Output: 192.168.1.6

If using grep that supports Perl regex:

(your command that pulls mentioned line) | grep -Po 'inet \K[\d\.]+'
Related