Hi i am writing a perl. script to extract out the number of errors with each code as a summary. The input is like this

A_01: xxxxxxx xxxxxxxxxx xxx......... 1 violation A_02: xxxxxxx xxxxxxxxxx xxx......... 4 violations B_02: xxxxxxxx xxxxxxxxxx xxx......... 3 violations

So, from the input, basically i have to grep from A_01 until 1 violation, and from here to there is about 3 lines, and the output is like this:

A number of violations = 5; B number of violations = 3;

But, my code is only able to read the violations all though, and sum it out. My code is like tis:

open(DATA, "<abc.txt, $!"; $num = 0; while (<DATA>){ while (<DATA>){ if ( (($_ =~ /violation/) || ($_ =~ /violations/))) { if ($_ =~ /(\d+)/) { $num = $num + $1; } } } if ($num == 0) { print "DM0$x ....... CLEANED\n" } else { print "DM0$x ....... Total = $num violations\n" } }

In reply to grep the data out of a text file. by DespacitoPerl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.