This is kind of a kludge, and it ain't using fancy REs, but I think it does what you want.

#!/usr/bin/perl -pw use strict; use warnings; our ($prev1, $prev2); if (/DISK.*DISK/) { if ($prev1 =~ /cache/ && $prev2 =~ /cache/) { print "\n"; } } if ($prev1 =~ /DISK.*DISK/) { print "\n"; } $prev2 = $prev1; $prev1 = $_;

Example execution with sample data.

me@mybox:~/sandbox $ ./2.pl input.txt Use of uninitialized value in pattern match (m//) at ./2.pl line 14, < +> line 1. cache,23778,0.0,0,0.0,0,0.0,0.0,0.00 cache,205,0.0,0,0.0,0,0.0,0.0,0.00 DISK-UTIL-PEAK-HIGH:,54.3,DISK-UTIL:,2.5 PROC-Name,PID,PYS-IO-RATE,READs,READ-RATE,WRITEs cache,26094,0.9,1,0.9,0,0.0,0.0,0.00 cache,16046,0.0,0,0.0,0,0.0,0.0,0.00 cache,3848,0.0,0,0.0,0,0.0,0.0,0.00 cache,23778,0.0,0,0.0,0,0.0,0.0,0.00 cache,205,0.0,0,0.0,0,0.0,0.0,0.00 DISK-UTIL-PEAK-HIGH:,54.3,DISK-UTIL:,3.4 ### last line ### me@mybox:~/sandbox $ cat input.txt cache,23778,0.0,0,0.0,0,0.0,0.0,0.00 cache,205,0.0,0,0.0,0,0.0,0.0,0.00 DISK-UTIL-PEAK-HIGH:,54.3,DISK-UTIL:,2.5 PROC-Name,PID,PYS-IO-RATE,READs,READ-RATE,WRITEs cache,26094,0.9,1,0.9,0,0.0,0.0,0.00 cache,16046,0.0,0,0.0,0,0.0,0.0,0.00 cache,3848,0.0,0,0.0,0,0.0,0.0,0.00 cache,23778,0.0,0,0.0,0,0.0,0.0,0.00 cache,205,0.0,0,0.0,0,0.0,0.0,0.00 DISK-UTIL-PEAK-HIGH:,54.3,DISK-UTIL:,3.4 ### last line ###

FYI, what I think you're asking for is: if "cache" is on 2 consecutive lines followed by "DISK" twice on the 3rd consecutive line, then insert a newline before and after the "DISK" line except the last newline at the end.


In reply to Re: regexp look behind? by jffry
in thread regexp look behind? by dbs

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.