Cloud Computing and Technology Management Insight from Brian Parsons rss

Find IP Addresses with awk

June 27, 2012 at 06:00 PM    

I needed to find an IP address amidst a bunch of random text. I googled and didn't find anything that worked for me the way I needed, so I made my own with awk. I thought I'd put it up here in case someone else could use it:

awk '{match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/); ip = substr($0,RSTART,RLENGTH); print ip}'

So you can pipe anything through this and it will grab the IP (V4) address(es). Example:

$ ifconfig | awk '{match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/); ip = substr($0,RSTART,RLENGTH); print ip}'

blog comments powered by Disqus

This is the personal web site of Brian Parsons. The views and opinions expressed in essays written on this web site are my own and are in no way related to or reflective of any company that I am associated with.

Creative Commons License Unless otherwise noted, this work is licensed under the Creative Commons Attribution 3.0 Unported License.

Other Work By Brian