Re: extracting an IPv4 address from text?
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Sun, 03 Apr 2022 02:59:14 UTC
It appears that Robert Huff <roberthuff@rcn.com> said: > >Hello: > Let's suppose I want to parse a line from auth.log and extract >the IP address (if any) to stdout. As we've seen, there's lots of ways to do it. Here's a one liner using only commands in the base system: sed -E -n 's/(^|.*[^0-9])([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*/\2/p' auth.log | sort -u > ipaddrs The pattern for IP address isn't exactly right since it doesn't limit the numbers to 0-255 and will match 999.0999.9999.999 but I think it's close enough in practice. I know how to write a regular expression that just matches the right range of numbers but it's really long and ugly. The "sort -u" gets rid of duplicates. R's, John