Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Help with grep function

Reply
Thread Tools

Help with grep function

 
 
Deepu
Guest
Posts: n/a
 
      07-11-2006
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

 
Reply With Quote
 
 
 
 
axel@white-eagle.invalid.uk
Guest
Posts: n/a
 
      07-11-2006
Deepu <> wrote:
> 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.


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

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

It's not really a Perl question.

Axel


 
Reply With Quote
 
 
 
 
David Squire
Guest
Posts: n/a
 
      07-11-2006
Deepu wrote:
> 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

----


DS

 
Reply With Quote
 
Xicheng Jia
Guest
Posts: n/a
 
      07-11-2006
Deepu wrote:
> 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

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      07-11-2006
Deepu <> wrote:

> 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


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Xicheng Jia
Guest
Posts: n/a
 
      07-11-2006
Keith Thompson wrote:
> "Xicheng Jia" <> writes:
> >
> > 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>;
>


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with grep function perlUSER Perl Misc 10 04-29-2006 12:26 PM
Want to use equivalent of grep function in perl perlmbk Perl Misc 3 11-10-2005 12:20 PM
Should I write my own Grep or Index function ? Mark Perl Misc 3 07-07-2005 12:32 PM
writing a function like map, sort, grep John Perl Misc 4 11-02-2004 11:17 PM
Pattern matching help! grep emails from file! danpres2k Perl 3 08-25-2003 02:47 PM



Advertisments