hi,
I'm dealing with a problem in which I have to scan through a short text and print out all ip addresses within it.
assume I have a text stored in a file named 'test', and I write a small perl program which read all lines of the text and print out any ip address:
open FILE, "test" or die $!; -
open FILE, "header" or die $!;
-
-
print "IP addresses found:\n";
-
while ($line = <FILE>){
-
-
if ($line=~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/) {
-
print $1;
-
print "\n";
-
}
-
}
-
There is a problem, however, that the perl program can print only 1 ip on each line. it's ok if there are all ip addresses in the same line are similar, yet if there are different ips on 1 line, we only get 1 ip but miss the others.
example:
"Hello this is the msg from 192.168.1.1 blah blah... 192.168.1.2
This is the second text from 192.168.1.3 blah blah..."
will print out only 192.168.1.1 and 192.168.1.3, and we'll miss the 192.168.1.2
any suggestion :-?
thanx :D
11 7499
Try the /g modifier on your regexp to progressively search. You'll have to fiddle with your code a bit, but it shouldn't be too bad.
Try the /g modifier on your regexp to progressively search. You'll have to fiddle with your code a bit, but it shouldn't be too bad.
err, how is it? perl is pretty new to me though I ppl say it's just like php. is there any more optimal way to read thru the whole text and pick up what we want?
err, how is it? perl is pretty new to me though I ppl say it's just like php. is there any more optimal way to read thru the whole text and pick up what we want?
What Ganon is referring to is taking your regular expression and making it lok like this: -
$line=~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/g
-
The /g will apply globally. If you look at any Perl regex tutorial, they should explain the characters after the regex. They are simply options to the regex as a whole.
Also, it looks like you are cycling through the file fine, line by line. Its a slightly slower way to do it, but slurping the entire file into an array or something tends to be ram heavy for larger files.
Regards,
Jeff
it still doesnot work, the new regular expression can only print the first thing that matches the pattern, then it ignore the rest of that line. does anybody know how to force it read until the end of line and print whatever matching.
is there any way to read the whole text but not line by line? can we read word by word instead? does a string work in this case?
it still doesnot work, the new regular expression can only print the first thing that matches the pattern, then it ignore the rest of that line. does anybody know how to force it read until the end of line and print whatever matching.
is there any way to read the whole text but not line by line? a string works in this case?
That should work. But, remember that though /g forces the pattern match to match all occurences, the if() loop will execute only once. You should be using while() loop. -
while ($line=~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/g) {
-
print $1;
-
print "\n";
-
}
-
-
- Nithin
is there any way to read the whole text but not line by line? can we read word by word instead? does a string work in this case?
Inorder to read whole text from the file, you need to undefine input record separator ($/) which is "\n" by default ,before reading from file. -
$/ = "";
-
while(<FILE>) { ## read entire file
-
it works like a charm when while loop is used instead of if. I see the problem: 'if' will quit immediately as soon as a match is found, yet 'while' continues until reaching the end of line :D
actually I see there's no need to set $\=""; still works well;
thx so much!
actually I see there's no need to set $\=""; still works well;
thx so much!
Yes, offcourse. That was just to answer your second question.
errr, another problem raised:
assume that there are some similar ip addresses found duplicated in the text: ip1, ip1, ip2, ip2, ip2, ip3... how can we filter and get each ip address printed once only (i.e eliminate the duplicated ips)
asedt is correct. See the Perlmonks link and you will see the following code in one of the responses there: -
my @array = ("abc","def","abc","ghi","ghi","abc","jklm","abc","def");
-
my %hash = map { $_ => 1 } @array;
-
my @unique = keys %hash;
-
So, you would simply create an array that contains all of the IPs, then the hash would be created from that array. Since the keys in an array have to be unique, it will not create a new key. In the end, print the list of keys.
Regards,
Jeff
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Robin Cull |
last post by:
Imagine I have a dict looking something like this:
myDict = {"key 1": , "key
2": , "key 3": ,
"key 4": }
That is, a set of keys which have a variable length list of associated
values after...
|
by: Shmulik |
last post by:
How can I (can I?) use a regular expression as the filter to a
FileSystemWatcher?
I want to watch for something like this:
Regex regex = new Regex(@"^Current(One|Two|Three)File\.txt$",...
|
by: Lou |
last post by:
I'm using the Response Filter Class in MS KB article 811162
(http://support.microsoft.com/default.aspx?scid=kb;EN-US;811162) to
generate a static htm page from an asp.net template page being...
|
by: dick |
last post by:
I am just trying to print/report the results of a "filter by
selection" which is done by right-clicking a form, filling in values,
and "applying the filter."
I have searched the newsgroups, and...
|
by: Salad |
last post by:
I have a log file with a list of records. The log file can be
unfiltered or filtered.
I have a command button to call a data entry form from the log. At
first I was only going to present the...
|
by: Anonieko Ramos |
last post by:
Answer. Use IHttpHandler.
thanks Ro ry for coming up with this code.
It processes css file to add variables. neat idea
using System;
using System.IO;
using System.Text;
using...
|
by: Leo |
last post by:
I am using OPENFILENAME and GetSaveFileName to let user save a
document out. However, the default name won't show up in the dialog and
the selected filter can't be obtained (always got the default...
|
by: ocbka1 |
last post by:
i'm using creating a webpage on the fly that i save as an xls file to
be attached to an email and sent dynamically. i've got a custom
response filter class to write it out.
the problems start...
|
by: Ron S |
last post by:
After days of searching I finally an example that would work with my application, the only problem is after entering all of the code it is not working. Would someone be kind enough to take a look at...
|
by: franc sutherland |
last post by:
Hello,
I have a report which I filter using the me.filter command in the
OnOpen event.
Me.Filter = "OrderID=" & Forms!variable_form_name!
Me.FilterOn = True
I want to be able to open that...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |