Re: The regex to capture IPv4 addresses from a file by using (e)grep.



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
.



Relevant Pages

  • The Cat Came Back / Unnecessary Services?
    ... But de cat came back, he couldn't stay no long-er, ... Logical Disk Manager Watchdog Service ... Network Connections ... Remote Access Connection Manager ...
    (microsoft.public.win2000.general)
  • Re: IPC$ Problem
    ... >to enter a password to connect to IPC$. ... There's no correct response to the IPC$ prompt. ... Run the Network Setup Wizard on the XP computer, ... Enable the Guest account for network access by typing this line at ...
    (microsoft.public.windowsxp.network_web)
  • Re: mdw Network issue?
    ... Does Access actually open, and then they get this prompt, or is it before ... Access opens? ... > All of the network drives are set on log on using an NT ... > When i open acces (no command line parameters)it is now ...
    (microsoft.public.access.security)
  • Re: Access denied ! Shouldnt XP ask for username & password inste
    ... Disabling the Windows XP login prompt for password ... How to Setup Windows, Network, VPN & Remote Access on ... Assuming computer A username is called Auser and computer B has Buser. ...
    (microsoft.public.windowsxp.network_web)
  • Re: soho setup? firewall?
    ... When you say high quality Cat 5. ... need to upgrade all the NICS to Gigabit Ethernet NICs. ... I'm not sure there is any reason for cat6 in a small network other ...
    (microsoft.public.windows.server.sbs)