in reply to regexp look behind?

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.