Skip site navigation (1)Skip section navigation (2)
Date:      2 Apr 2022 22:59:14 -0400
From:      "John Levine" <johnl@iecc.com>
To:        freebsd-questions@freebsd.org
Cc:        roberthuff@rcn.com
Subject:   Re: extracting an IPv4 address from text?
Message-ID:  <20220403025915.002513A46478@ary.qy>
In-Reply-To: <25160.44484.392802.868667@jerusalem.litteratus.org>

next in thread | previous in thread | raw e-mail | index | archive | help

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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20220403025915.002513A46478>