Perl regular expression for exclude specific ip range

I need regular expression for excluding specific range.
e.g. Input Data is

10.10.10.50
67.172.15.15
10.10.10.15
78.122.105.108

I would like to extract only 67.172.15.15 & 78.122.105.108.
I tried with something like /(^10.10.10)/ but it's not working.
Please help me on this

Try something like:

awk '!/10.10.10/' file

Regards

Can you try something like:

  1. Put all the ip addresses in a file say file1.

grep -v "10.10.10" file1

HTH,:cool:

Regards,

Praveen

Franklin thanks for your reply.
I forgot to mention that , i am using regular expresion in perl and php.So awk solution will not work for me.

Can't you use the grep solution of sunpraveen with perl?

Perl grep require regular expression.
I think "grep -v" option given by sunpraveen will work only on shell.
I am using php function like preg_grep("/^(10.10.10.*)/",$ip_array).
So it requires pure regular explression.

I hope someone who's familiar with perl can give you the solution since I'm not familiar with it.

Regards

this should work for perl

grep(!/10.10.10/, @IP_ADD);

Thanks anchal
My perl problem solved using your solution.however it's not working in php.
Some times i need to switch to php script. So i also need solution for php

foreach ( source_of_ips )
{
  unless ( /10.10.10/ )
  {
     print "$_\n"
  }
}

If this doesn't work please post the section of the code in question.

My part of php code is as follows

// Get all ip's from router traffic data
$all_ips = read_file($all_traffic);

$local_ip = preg_grep("/10.10.10.*/",$all_ips);

//Remote ips are ip excluding ip range 10.10.10.*
$remote_ips = preg_grep("!/10.10.10/",$ip_arr);

//Display all Remote IP's
foreach($remote_ips as $key=>$ip)
{
	echo $ip;
}

In the {} of your foreach loop, can you nest an if statement with a regex to filter out the local addresses? I do most of my programming in perl or ksh/bash so I'm not sure about the PHP syntax. Try this for the if condition:

if ( ! /10\.10\.10\. )
{
code
}

The . is a regex meta character and should be quoted in regular expressions when you use them in a literal sense.

One way to do it in Perl, if your data is in a file:

$ 
$ cat f1
10.10.10.50
67.172.15.15
10.10.10.15
78.122.105.108
$ 
$ perl -nle 'print if !/^10.10.10./' f1
67.172.15.15
78.122.105.108
$ 
$ 

tyler_durden

Reverse match in PHP is not as straight forward as in Perl or awk. You have to use positive and negative "lookbehind" assertions.

That's what I would do in PHP:

<?php
// Get all ip's from router traffic data
$all_ips = file_get_contents("fileWithAllIps");

preg_match_all("/^(?!10.10.10.).+/m", $all_ips, $remote_ips);
preg_match_all("/^(?=10.10.10.).+/m", $all_ips, $local_ips);

echo "<pre>";
echo "Local IP's\n";
foreach($local_ips[0] as $key=>$ip)
{
		echo $ip."\n";
}

echo "Remote IP's\n";
foreach($remote_ips[0] as $key=>$ip)
{
		echo $ip."\n";
}
?>

---------- Post updated at 10:08 PM ---------- Previous update was at 09:55 PM ----------

Another PHP approach without lookbehind assertions:

$all_ips = file("file");

echo "Local IP's\n";
foreach($all_ips as $ip){
	if (preg_match("/^10.10.10\..+$/", $ip)){
		$local_ips[] = $ip;
	} else {
		$remote_ips[] = $ip;
	}
}

foreach($local_ips as $key=>$ip)
{
		echo $ip."\n";
}

echo "Remote IP's\n";
foreach($remote_ips as $key=>$ip)
{
		echo $ip."\n";
}

Thanks ripat. Your solution works for me. :slight_smile:

using sed also we can solve this problem:

 sed -n /^[^1]/p ip 
# cat infile |  perl -e 'while (<>) { @arr= split(/[.]/); if (@arr[0,1,2]!= (10,10,10)) { print "$_"; } }'

the output is:
67.172.15.15
78.122.105.108

If you store the given IP into a variable called $ip.

Then,you can use the following statement to print the IP in the range specified.

print "IP:$ip" unless /^10.10.*/;