Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

find a word in a file

Status
Not open for further replies.

donny750

Programmer
Joined
Jul 13, 2006
Messages
145
Location
FR
hi,

I've a file text and i want to find how many times i've this
Code:
|WARNING|
in my file.

How can i do ?
I unix i use grep and perl ??

Thanks
 
Code:
my @arr = `grep '|WARNING|' <filename>` ;
print scalar(@arr) ;

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Not using an array:
Code:
use strict;
use warnings;
my $count;

while (<>) {
   $count++ if (/\|WARNING\|/);
}

print $count;

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::PerlDesignPatterns)[/small]
 
thanks
i've found and make this
Code:
#!/usr/bin/perl

use strict;

my $numbers = 0;
my $infile = 'example.log';
open DATA, $infile or die $!;
foreach  my $intext (<DATA>) {
if ( $intext =~ m/|WARNING\/ )
{
$numbers ++;
}
}
        
        print "Found it : ". $numbers ."\n";
 
better written as:


Code:
#!/usr/bin/perl

use strict;
my $numbers = 0;
my $infile = 'example.log';
open DATA, $infile or die $!;
while(my $intext = <DATA>) {
   $numbers++ while ($intext =~ m/\|WARNING\|/g);
}
print "Found it : ". $numbers ."\n";

- Kevin, perl coder unexceptional!
 
try that again:

Code:
#!/usr/bin/perl

use strict;
my $numbers = 0;
my $infile = 'example.log';
open DATA, $infile or die $!;
while(my $intext = <DATA>) {
   $numbers++ while ($intext =~ m/\|WARNING\|/g);
}
print "Found it : ". $numbers ."\n";

- Kevin, perl coder unexceptional!

- Kevin, perl coder unexceptional!
 
thanks kevin for your
I want to learn the perl and i learn a lot there
while your solution is better ?
She find if there are two expression in the same line ??
 
i try your solution and it s better
if i've 2 or 3 times the expression in the same line
she find it
beacause you have had "/g" ???
thanks
 
donny750, you had this:

Code:
if ( $intext =~ m/|WARNING\/ )
{
$numbers ++;
}

which is not correct. The pipe character '|' is a special character in perl that means 'or', as in: 'this or that' (this|that). If you are searching for a pipe in a string you have to escape it with backslash \| so perl doesn't think it means 'or'.

and yes, the 'g' modifier means to find all occurances of the pattern. Using a 'while' loop with the 'g' modifier allows you to increment a counter for all the occurances that match:

Code:
$numbers++ while ($intext =~ m/\|WARNING\|/g);

- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top