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

Hi all

I have a question about searching in Perl

For example :

Lets say a have a text file like

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

RE: Searching and printing


open(FILE,"ip.txt") || die("failed to open file:$!");

$found = 0;
foreach $line (@data) {
   if ($line eq "") {
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 cherubini
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director

RE: Searching and printing


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,"";
push @list,"";
push @list,"";
push @list,"";
push @list,"";
push @list,"";
push @list,"";
push @list,"";

$arg = "";

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

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


RE: Searching and printing

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

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

$ip = '';
while ($buffer =~ /$ip/gs)
print "Found $ip\n";

Good Luck

 keep the rudder amid ship and beware the odd typo

RE: Searching and printing

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