Re: Help with grep function



"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.
.