Help with grep function

Discussion in 'Perl Misc' started by Deepu, Jul 11, 2006.

  1. Deepu

    Deepu Guest

    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
     
    Deepu, Jul 11, 2006
    #1
    1. Advertising

  2. Deepu

    Guest

    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
     
    , Jul 11, 2006
    #2
    1. Advertising

  3. Deepu

    David Squire Guest

    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
     
    David Squire, Jul 11, 2006
    #3
  4. Deepu

    Xicheng Jia Guest

    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
     
    Xicheng Jia, Jul 11, 2006
    #4
  5. 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
     
    Tad McClellan, Jul 11, 2006
    #5
  6. Deepu

    Xicheng Jia Guest

    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
     
    Xicheng Jia, Jul 11, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. danpres2k
    Replies:
    3
    Views:
    7,475
    danpres2k
    Aug 25, 2003
  2. John
    Replies:
    4
    Views:
    122
    Ben Morrow
    Nov 2, 2004
  3. Mark
    Replies:
    3
    Views:
    109
    Tad McClellan
    Jul 7, 2005
  4. perlmbk
    Replies:
    3
    Views:
    559
  5. perlUSER

    Need help with grep function

    perlUSER, Apr 28, 2006, in forum: Perl Misc
    Replies:
    10
    Views:
    143
    perlUSER
    Apr 29, 2006
Loading...

Share This Page