36
16
With the following grep
syntax I want to match all IP address in a file (from a ksh
script)
grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' file
The problem: It also matches words (IP) that have more then 4 octets:
1.1.1.1.1
or
192.1.1.1.160
How can I match a valid IP and only IP addresses with 4 octets? I can also use Perl – a one line syntax solution, if grep
doesn't work.
Have a look at my answer in unix stackexchange: https://unix.stackexchange.com/a/389565/249079
– Ganapathy – 2017-09-29T05:35:23.4034It will match
999.999.999.999
too. – cYrus – 2010-10-24T12:11:04.2475
So, you only want to grep IPv4 addresses, right?
– Arjan – 2010-10-24T12:51:55.750And as for you
192.1.1.1.160
example: would you expect192.1.1.1
or1.1.1.160
or no match at all? – Arjan – 2010-10-24T12:54:56.580about 192.1.1.1 and 1.1.1.160 they valid IP I accept – jennifer – 2010-10-24T12:56:37.657
Errr? So, if the input is
– Arjan – 2010-10-24T13:04:24.103192.1.1.1.160
then you just want192.1.1.1
in the output? Then you don't have a problem (except for matching 999.999.999.999 an so on too), but just need the-o
command line option to only print the actual matches, not the full lines?5Technically, IP addresses such as
192.1.4097
are valid and accepted by Linux glibc and Windows. – user1686 – 2010-10-24T14:49:35.1071
Ah, I never knew!
– Arjan – 2010-10-24T15:15:59.207ping 2130706433
, on OS X:PING 2130706433 (127.0.0.1): 56 data bytes
.1@Arjan:
0x7f.1
and0177.1
– user1686 – 2010-11-07T10:23:14.047