From: Hongyi Zhao on
Hi all,

I want to obtain all of the IPv4 addresses from a file by using
(e)grep. What regex should I use to do this thing?

Thanks in adavance.
--
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
From: Edgardo Portal on
On 2009-12-08, Hongyi Zhao <hongyi.zhao(a)gmail.com> wrote:
> Hi all,
>
> I want to obtain all of the IPv4 addresses from a file by using
> (e)grep. What regex should I use to do this thing?
>
> Thanks in adavance.

Though not strictly correct (e.g., it'd find 260.1.1.1, 256.1.1.1, etc.), but
how about the following as a rough start?

prompt$ cat /tmp/ips.tmp
192.168.142.138
5.4
66.33.154.1
127.0.0.1
1.888.555.1212

prompt$ cat /tmp/ips.tmp \
| egrep '[12]?[0-9]?[0-9]\.[12]?[0-9]?[0-9]\.[12]?[0-9]?[0-9]\.[12]?[0-9]?[0-9]'
192.168.142.138
66.33.154.1
127.0.0.1

From: pk on
Hongyi Zhao wrote:

> Hi all,
>
> I want to obtain all of the IPv4 addresses from a file by using
> (e)grep. What regex should I use to do this thing?

\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|
[01]?[0-9][0-9]?)\b

on a single line. GNU egrep should support \b, but I'm not sure.
From: Hongyi Zhao on
On Tue, 08 Dec 2009 20:14:42 +0000, pk <pk(a)pk.invalid> wrote:

>\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|
>[01]?[0-9][0-9]?)\b
>
>on a single line. GNU egrep should support \b, but I'm not sure.

I've tried the above code, but I'll get nothing to output. See the
following minimal example:

$ cat -vte ips
192.168.142.138$
5.4$
66.33.154.1$
127.0.0.1$
1.888.555.1212
$ egrep
'\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0
-9]|[01]?[0-9][0-9]?)\b' ips

Best regards.
--
..: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.
From: Ben Bacarisse on
Hongyi Zhao <hongyi.zhao(a)gmail.com> writes:

> On Tue, 08 Dec 2009 20:14:42 +0000, pk <pk(a)pk.invalid> wrote:
>
>>\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|
>>[01]?[0-9][0-9]?)\b
>>
>>on a single line. GNU egrep should support \b, but I'm not sure.
>
> I've tried the above code, but I'll get nothing to output. See the
> following minimal example:
>
> $ cat -vte ips
> 192.168.142.138$
> 5.4$
> 66.33.154.1$
> 127.0.0.1$
> 1.888.555.1212
> $ egrep
> '\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0
> -9]|[01]?[0-9][0-9]?)\b' ips

On my system the problem is the (?:) syntax. This is from Perl REs
and denotes a non-capturing group. I find that

egrep '\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b' ips

works fine.

--
Ben.