#!/usr/bin/perl use strict; use warnings; my $scanword; my $wk1; my $wk2; my $wk3; my $wk4; my $wk5; my $wk6; my @elements; my $i; my $n1; my $inrec1='0;1;2;3;4;5;6?link=misc/redirect/ekey=BOZOTHECLOWN&dmsdate=20150210;7;8;9'; my $inrec2='0;1;2?link=misc/redirect/ekey=BOZOTHECLOWN&dmsdate=20150210;3;4;5;6;7;8;9'; my $outrec; ##### sub special() { $wk1=''; $wk2=''; $wk3=''; $wk4=''; $wk5=''; $wk6=''; #reset vars $wk1=$elements[$i]; #field number to scan $a=index($wk1,"?"); #look for ? if ( $a != -1 ) { #? was found #Look for $scanword $wk2=index($wk1,"$scanword"); #find start of scanword. I.E. "ekey=" $wk5=index($wk1,"=",$wk2); #find start of "=" $wk3=index($wk1,"&",$wk2); #find start of next & if ( $wk2 != -1 ) { #found scanword? if ( $wk3 == -1) { $wk3 = length ($wk1); } #default to length of string if ampersand not found $wk4 = substr($wk1,$wk2,$wk3-$wk2); #wk1 is the field, wk2 is start of scanword, wk3 is the end position $wk6 = substr($wk1,$wk5+1,$wk3-$wk5-1); #wk1 is the field, wk5+1 is byte after = in the scanword, wk3 is the end position print STDOUT "array element = $elements[$i] \n"; print STDOUT "Found ? in the field at offset $a \n"; print STDOUT "Found $scanword in the field at offset $wk2 \n"; print STDOUT "Found end in the field at offset $wk3 \n"; print STDOUT "Field wk4 = $wk4 \n"; print STDOUT "Field wk6 = $wk6 \n"; $b=$wk3-$wk5-1; #length to blank substr($elements[$i],$wk5+1,$wk3-$wk5-1) = ' ' x $b; #move blanks to array element } } } ##### print STDOUT "inrec1=$inrec1 \n"; #print inrec @elements = split(';', $inrec1, -1); #split by semicolon, -1 means to keep trailing fields if empty $i=2; #element offset $scanword="ekey="; #text to scan for special(); #call routine $n1=$wk6; #what was stripped out push (@elements, $n1); #add to end of array $outrec = join(";",@elements); #convert to output record print STDOUT "outrec=$outrec \n"; #print output record print STDOUT "inrec2=$inrec2 \n"; #print inrec @elements = split(';', $inrec2, -1); #split by semicolon, -1 means to keep trailing fields if empty $i=2; #element offset $scanword="ekey="; #text to scan for special(); #call routine $n1=$wk6; #what was stripped out push (@elements, $n1); #add to end of array $outrec = join(";",@elements); #convert to output record print STDOUT "outrec=$outrec \n"; #print output record exit; OUTPUT EXAMPLES: ekey is not in elements[2] so do nothing inrec1=0;1;2;3;4;5;6?link=misc/redirect/ekey=BOZOTHECLOWN&dmsdate=20150210;7;8;9 outrec=0;1;2;3;4;5;6?link=misc/redirect/ekey=BOZOTHECLOWN&dmsdate=20150210;7;8;9; ekey is in elements[2] so blank it in input record and add move following text BOZOTHECLOWN to a new element in array inrec2=0;1;2?link=misc/redirect/ekey=BOZOTHECLOWN&dmsdate=20150210;3;4;5;6;7;8;9 array element = 2?link=misc/redirect/ekey=BOZOTHECLOWN&dmsdate=20150210 Found ? in the field at offset 1 Found ekey= in the field at offset 21 Found end in the field at offset 38 Field wk4 = ekey=BOZOTHECLOWN Field wk6 = BOZOTHECLOWN outrec=0;1;2?link=misc/redirect/ekey= &dmsdate=20150210;3;4;5;6;7;8;9;BOZOTHECLOWN