Help with grep function

D

Deepu

Hi All,

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.

Thanks
 
A

axel

Deepu said:
I am trying to count the number of errors in a file:
ERROR: Display
ERROR:Virtual
ERROR: Test
ERROR: Random
$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.

Have a look at the -v option to grep...

`grep -i error filename | grep -vci virtual`

It's not really a Perl question.

Axel
 
D

David Squire

Deepu said:
Hi All,

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.

You could try using Perl's own grep function, rather than making a
system call. This would also make your program more portable. On the
other hand, you don't need to store the matching lines, so grep is not
needed. How about this:

----

#!/usr/bin/perl
use strict;
use warnings;

my $TotalErrors;
my $NonVirtualErrors;
# here you would open your file, e.g. open my $FileHandle, '<',
'filename' or die "Couldn't open filename: $!";
# then use $FileHandle instead of DATA below
while (my $Line = lc(<DATA>)) {
next unless $Line =~ /error/;
$TotalErrors++;
$NonVirtualErrors++ unless $Line =~ /virtual/;
}
print "Total errors: $TotalErrors\nNon-virtual errors: $NonVirtualErrors";

__DATA__
ERROR: Display
ERROR:Virtual
ERROR: Test
ERROR: Random

----

Output:

Total errors: 4
Non-virtual errors: 3
 
X

Xicheng Jia

Deepu said:
Hi All,

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>;

Xicheng
 
T

Tad McClellan

Deepu said:
I am trying to count the number of errors in a file:
I use:

$ctError = `grep -ci error filename`;
Now i am trying to count error which doesnot contain Virtual,


$ctError = grep !/virtual/i, `grep -ci error filename`;

or, probably less confusing:

$ctError = grep !/virtual/i, qx/grep -ci error filename/;



Or don't use grep(1) at all, and do it all with (tricky) Perl:

perl -lne '$cnt++ if /error/i and not /virtual/i }{ print $cnt' filename
 
X

Xicheng Jia

Keith said:
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>;

these two statements may do different things. i.e. that single regex
will match:

ERROR: TEST Virtual

If OP wants to do grep -v, then my previous solution is not correct. in
a single regex, it can be:

my $count = grep /^(?=.*?ERROR)(?!.*Virtual)/i, <$fh>;

Xicheng
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top