×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Searching and printing

Searching and printing

Searching and printing

(OP)
Hi all

I have a question about searching in Perl

For example :

Lets say a have a text file like

212.12.12.2
199.2.3.1
210.3.3.2
212.12.12.2
199.65.78.123
112.34.55.6
212.12.12.2
199.88.77.12

What I want to do know is to find out how many lines that contains ip number 212.12.12.2.
It´s 3 okej, but how do I get Perl to understands it ?
 

RE: Searching and printing

Here:


open(FILE,"ip.txt") || die("failed to open file:$!");
@data=<FILE>;
close(FILE);

$found = 0;
foreach $line (@data) {
   if ($line eq "212.12.12.2") {
       $found++;
   }
}
print "$found\n";


You can replace the IP address with a variable so that you can search more than one IP.

Hope this helps,

-Vic

vic cherubini
vikter@epicsoftware.com
www.epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
====

RE: Searching and printing

Hi,


I like to use the "GREP" function: you could write it as follows(I've put the values in a array for testing):

push @list,"212.12.12.2";
push @list,"199.2.3.1";
push @list,"210.3.3.2";
push @list,"212.12.12.2";
push @list,"199.65.78.123";
push @list,"112.34.55.6";
push @list,"212.12.12.2";
push @list,"199.88.77.12";

$arg = "212.12.12.2";

$total = grep { /$arg/ } @list;

print "Value for \$total is $total\n";


Thierry

RE: Searching and printing

Or, (TMTOWTDI).....
you could use regex's....

open(FILE,"ip.txt") || die("failed to open  file:$!");
while (<FILE>) { $buffer .= $_; }
close(FILE);

$ip = '212.12.12.2';
while ($buffer =~ /$ip/gs)
{
print "Found $ip\n";
$count++;
}

Good Luck


 
 
 keep the rudder amid ship and beware the odd typo

RE: Searching and printing

(OP)
Thanks all
Works fine

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close