Re: The regex to capture IPv4 addresses from a file by using (e)grep.
- From: Stephane CHAZELAS <stephane_chazelas@xxxxxxxx>
- Date: Sat, 12 Dec 2009 11:44:40 +0000 (UTC)
2009-12-8, 14:25(+00), Edgardo Portal:
On 2009-12-08, Hongyi Zhao <hongyi.zhao@xxxxxxxxx> 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
5.4 is a valid IP address, typically for an interface on a class
A network, a.b.c.d is just the quad-decimal notation.
5.4 is the same as 5.0.0.4. 127.0.0.1 is more conveniently
written 127.1 (the first address on the 127.0/8 network).
If you want to accept all the forms suppored by inet_addr(3) or
gethostbyname(3) or getaddrinfo(3), there'll be more than that.
Like
127.0.0.1, 0177.0.1, 0x7f000001 are all the same IP address, and
09.09.09.09 is invalid (wrong octal numbers).
For inet_pton(3), that's another matter, that one only supports
quad-decimal with 1 to 3 digits per number.
--
Stéphane
.
- References:
- The regex to capture IPv4 addresses from a file by using (e)grep.
- From: Hongyi Zhao
- Re: The regex to capture IPv4 addresses from a file by using (e)grep.
- From: Edgardo Portal
- The regex to capture IPv4 addresses from a file by using (e)grep.
- Prev by Date: Find age of a directory
- Next by Date: Re: The regex to capture IPv4 addresses from a file by using (e)grep.
- Previous by thread: Re: The regex to capture IPv4 addresses from a file by using (e)grep.
- Next by thread: Re: The regex to capture IPv4 addresses from a file by using (e)grep.
- Index(es):
Relevant Pages
|