Perl regular expression for exclude specific ip range


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl regular expression for exclude specific ip range
# 8  
Old 09-09-2009
this should work for perl

Code:
grep(!/10.10.10/, @IP_ADD);

# 9  
Old 09-09-2009
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
# 10  
Old 09-09-2009
Code:
foreach ( source_of_ips )
{
  unless ( /10.10.10/ )
  {
     print "$_\n"
  }
}

If this doesn't work please post the section of the code in question.
# 11  
Old 09-10-2009
My part of php code is as follows

Code:
// 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;
}

# 12  
Old 09-13-2009
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:

Code:
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.
# 13  
Old 09-13-2009
One way to do it in Perl, if your data is in a file:

Code:
$ 
$ 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
# 14  
Old 09-13-2009
Quote:
Originally Posted by nrbhole
My part of php code is as follows

Code:
$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;
}

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:
Code:
<?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:

Code:
$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";
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl regular expression

Hi , I have the below array my @actionText = ("delivered to governor on 21/23/3345" , "deliver jllj" , "ram 2345/43"); When i am trying to grep the contents of array and if mathced substituting with the digitis or some date format from the element like below my @action = grep { $_ =~... (7 Replies)
Discussion started by: ragilla
7 Replies

2. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

3. Shell Programming and Scripting

Perl regular expression help!

Hi I am doing something basic like... if ($stringvariable =~ /have not typed/) I have a little problem because the 'not' in the expression gets highlighted as a kind of a '!'..what am I supposed to do in this situation? Thank you ---------- Post updated at 03:24 PM ----------... (1 Reply)
Discussion started by: vas28r13
1 Replies

4. Shell Programming and Scripting

Hidden Characters in Regular Expression Matching Perl - Perl Newbie

I am completely new to perl programming. My father is helping me learn said programming language. However, I am stuck on one of the assignments he has given me, and I can't find very much help with it via google, either because I have a tiny attention span, or because I can be very very dense. ... (4 Replies)
Discussion started by: kittyluva2
4 Replies

5. Shell Programming and Scripting

Perl Regular Expression

Hello, I am trying to use perl LWP module to read and get a specfic URL page. The issue is that the URL ends with the data and time and time is not consistent it changes all the time. if anyone could help me how to write a regular expressin that would work in the LWP::UserAgent get function to... (0 Replies)
Discussion started by: bataf
0 Replies

6. Shell Programming and Scripting

Regular expression in Perl

Hi, I need and expression for a word like abc_xyz_ykklm The expresion should indicate that the word starts with abc and end with ykklm but does not contain xyz string in the middle. Example: abc_tmn_ykklm is ok and abc_xyz_ykklm is not Ok. Please help. Regards. (1 Reply)
Discussion started by: asth
1 Replies

7. Shell Programming and Scripting

Regular Expression to exclude pattern

Hi All I am using regular expressions to determine how to group certain data. I've included an example of the data below. USD_SPTR_2Y_725.5_PUT_EUROPEAN_09Q1|USD||European| CAD_NDX_10Yx1Y_5.5_PUT_EUROPEAN_09Q1|CAD||European| The regular expressions I am using is as follows and this is... (5 Replies)
Discussion started by: kingpin2502
5 Replies

8. Shell Programming and Scripting

regular expression in perl

hi, i want to extract the sessionID from this line. QnA Session Id : here the output should be-- QnA_SessionID=128589 Thanks NT (3 Replies)
Discussion started by: namishtiwari
3 Replies

9. Shell Programming and Scripting

SED: Print range, exclude regular expressions.

Ok, so I get that: sed -n '/START/,/END/p' file ...will print every line from START to END inclusive, but I don't want to see START or END. Apart from the obious: sed -n '/START/,/END/p' file | grep -v "START" | grep -v "END" ...is there a simpler way of doing this? Thanks as always! (2 Replies)
Discussion started by: cs03dmj
2 Replies

10. Shell Programming and Scripting

Regular expression help in perl

Hi all, I am trying to match a multi line string and return the matching string in one line. Here is the perl code that I wrote: #!/usr/bin/perl my $str='<title>My title</title>'; if ($str =~ /(<title>)(+)(<\/title>)/ ){ print "$2\n"; } It returns : My title I want the... (3 Replies)
Discussion started by: sdubey
3 Replies
Login or Register to Ask a Question