#!/usr/bin/perl
#/****************************************************************************\
#| |
#| Bryce Alcock |
#| This Script will read MULTI line patterns from a file. |
#| Once it finds a pattern it will write that pattern out, and will |
#| conditionally duplicate the pattern replacing on a line by line basis |
#| a sub pattern. |
#| ARGV[0] - the File to modify |
#| ARGV[1] - the pattern that triggers a duplicate chunk action. |
#| ARGV[2] - the pattern to replace ARGV[1] with. |
#| ARGV[3] - the pattern that governs the end of the multi line block |
#| Example: |
#| |
#| MultilineMatchDupReplace.pl inputFile.txt YY XY ENDBLOCK |
#| |
#| inputFile.txt: |
#| |
#| LINE1.... |
#| LINE2.... |
#| YY banana YY Apple YY YC. |
#| ENDBLOCK |
#| LINE1.... |
#| LINE2.... |
#| YP banana YP Apple YY YD. |
#| ENDBLOCK |
#| |
#| OUTPUT: |
#| |
#| LINE1.... |
#| LINE2.... |
#| YY banana YY Apple YY YC. |
#| ENDBLOCK |
#| LINE1.... |
#| LINE2.... |
#| XY banana XY Apple XY YC. |
#| ENDBLOCK |
#| LINE1.... |
#| LINE2.... |
#| YP banana YP Apple YY YD. |
#| ENDBLOCK |
#| |
#| WHAT IS THIS GOOD FOR: |
#| I use this to conditionally double the number and variety of input |
#| lines for various load test scripts. |
#| |
#| This can also be used as a building block for more complex replacement |
#| Conditionals. |
#\****************************************************************************/


# ARGV[0] is the file we are processing.
$File = $ARGV[0];
# Keep track of the number of lines read in,
# and the number of lines printed out.
$linesIN = 0;
$linesOUT = 0;
# Multi line Blocks, and number of multi line blocks we will duplicate.
$processingChunks=0;
$duplicates=0;

# a local parameter that indicates how many lines are in the multiline pattern.
$localLines = 0;

# ARGV[1] is the pattern to match.
# ARGV[2] is the replacement string.
# ARGV[3] is the Chunk end pattern, this defines the end of a multiline chunk.
$pattern=$ARGV[1];
$replacement=$ARGV[2];
$endpattern=$ARGV[3];

# temporary array to start each multi line chunk during processing.
@currentMultiLine;

# a state machine flag, 1 means we are gathering multi line data, 2 means
# we are printing it out, and conditionally duplicating it.
$state=1;

open(FILE,"+< $File");
while($line =<FILE>){
$linesIN++;

# if we match the end pattern,
# we should printing things out.
if($line =~ /$endpattern/){
# add the current line into the chunk.
$currentMultiLine[$localLines]=$line;
++$localLines;


++$processingChunks;
for($i=0;$i<$localLines;++$i){
print "$currentMultiLine[$i]";
# if we match the pattern, we are going to duplicate.
$compareLine = $currentMultiLine[$i];
if($compareLine =~ /OPERATOR/ || $printLine =~ /USERBANK/){
if($compareLine =~ /$pattern/){
$state=2;
}
}
}
#print "lines in Chunk: $localLines \n";
#print "Chunk No.: $processingChunks \n";
if($state==2){

$duplicates++;
#We FOUND A MATCH....
for($i=0;$i<$localLines;++$i){
$printLine = $currentMultiLine[$i];
if($printLine =~ /OPERATOR/ || $printLine =~ /USERBANK/){
$printLine =~ s/$pattern/$replacement/g;
}
print "$printLine";
}
}
$state=1;
$localLines=0;

}else{
$currentMultiLine[$localLines]=$line;
++$localLines;
}
}
print "\n\n\tJCL Requests Processed: $processingChunks duplicates $duplicates copied!\n";
close(FILE) or die "can't close $File $!";