Re: Help with grep function
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Tue, 11 Jul 2006 21:17:19 GMT
"Xicheng Jia" <xicheng@xxxxxxxxx> writes:
Deepu wrote:
I am trying to count the number of errors in a file:
ERROR: Display
ERROR:Virtual
ERROR: Test
ERROR: Random
I use:
$ctError = `grep -ci error filename`;
It gives 4
Now i am trying to count error which doesnot contain Virtual, so i
should get $ctError = 3. Is there any way possible to implement in the
same line.
my $count = grep /ERROR:(?!\s*Virtual)/i, <$fh>;
Note that it's not really necessary to force everything into a single
regular expression. For example:
my $count = grep { /ERROR:/i && !/Virtual/i } <$fh>;
Or, if you want to be even more verbose:
my $count = 0;
while (<$fh>) {
if (/ERROR:/i and not /Virtual/i) {
$count ++;
}
}
Which approach you use is a matter of taste.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.
- Follow-Ups:
- Re: Help with grep function
- From: Xicheng Jia
- Re: Help with grep function
- References:
- Help with grep function
- From: Deepu
- Re: Help with grep function
- From: Xicheng Jia
- Help with grep function
- Prev by Date: Re: Using References to Formats, Examining Scalars With Devel::Peek
- Next by Date: Re: processing large numbers/values/figures
- Previous by thread: Re: Help with grep function
- Next by thread: Re: Help with grep function
- Index(es):