Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

regexp look behind?

by dbs (Sexton)
on Nov 30, 2010 at 15:29 UTC ( #874518=perlquestion: print w/replies, xml ) Need Help??

dbs has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, How can I print a "\n" in between two different strings, cache and DISK.* where they appear twice.
__INPUT__ 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 ### __END__ use strict; use warnings; my $dstattmp = qq(/usr/local/log/disk.gpm.statlog.tmp); my $dstatlog = qq(/usr/local/log/disk.gpm.statlog.csv); open( my $DT, "+>>", $dstatlog ) || die "$dstatlog was not opened $!"; open( my $TD, "+>", $dstattmp ) || die "$dstattmp was not opened $!"; while (<$DT>) { next if ( $. == 1); if ( /cache|disk-.*/i ) { push my @tmp, split; print $TD join (",", @tmp),"\n"; } if ( /^(\D.*)(?=\d*)(?=disk-util-peak.*)/i ) print $TD "YES\n"; } ##if ( /^(cache.*)(?=\d*)(?=disk-util-peak.*)/i ) ##if ( /^(cache.*)(?=\d*)/ .. /disk-util-peak.*/igs ) } thank you!

Replies are listed 'Best First'.
Re: regexp look behind?
by raybies (Chaplain) on Nov 30, 2010 at 16:03 UTC

    keep track of your last line's match. If you'll wrap your regex in parenthesis like so  if (/(cache|disk-)/i), you can assign the match to a variable, like so  $lastmatch = $1;, then each pass through your loop, you then can  print "\n" if ($lastmatch ne $1 and "disk-" eq lc $1);

      thanks but no cigar! I would like the output to be
      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 "newline here" DISK-UTIL-PEAK-HIGH:,54.3,DISK-UTIL:,2.5 "newline here" 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 "newline here" DISK-UTIL-PEAK-HIGH:,54.3,DISK-UTIL:,3 ### last line ###

        No, thank you for finally clarifying just what you want the output to be!

Re: regexp look behind?
by jffry (Hermit) on Dec 01, 2010 at 00:47 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://874518]
Approved by Limbic~Region
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2021-04-23 16:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?