my $use = ""; my $sd = ""; my $count = 1; my $bset= 0; # if new SD was set,change to 1, 0 means it was not set. my $bend = 0; # means definition is ended } was found 1, 0 means still needs to close definition. open (TEXT_FILE, "services2.cfg'); while ($line = ) { print MYFILE $line; print $count ." - " . $line; if ($line =~ m/use/) { $bset = 0; $bend = 0; print "-----Reseting bset and bend\n"; $host = ; print MYFILE $host; $nextline3 = ; $nextline4 = ; $nextline5 = ; $nextline6 = ; $use = $line; $use =~ s/use//; $use = trim($use); $host =~ s/host_name//; $host = trim($host); print $count . ". ", color("white") . $host ."\n", color("reset"); # Should check if any of the lines already have service_description or } #could use a loop foreach ($nextline(i))...i++ #Line 3 if ($nextline3 =~ m/}/ ) { #service_description does not exist and was not set, we are in the last line, we add SD and } and define service of next printf ("(L3=}) Adding SD and }\n"); $sd = sdparse($use); print color("yellow"), "\n-->NEW SD: ". $sd ."\n", color("reset"); print MYFILE " service_description ". $sd."\n}\n\ndefine service{\n"; $bset = 1; $bend = 1; } if ($nextline3 =~ m/service_description/ && $bend eq 0) { print "(L3) SD here. \n"; print MYFILE $nextline3; $bset = 1; } if ($nextline3 !~ m/service_description/ && $bset eq 0 && $bend eq 0) { print MYFILE $nextline3; } if ($nextline4 =~ m/}/ && $bend eq 0 && $bset eq 0) { #service_description does not exist and was not set, we are in the last line, we add SD and } and define service of next printf ("(L4=}) Adding SD and }\n"); $sd = sdparse($use); print color("yellow"), "\n-->NEW SD: ". $sd ."\n", color("reset"); print MYFILE " service_description ". $sd."\n}\ndefine service{\n\n"; $bset = 1; $bend = 1; } if ($nextline4 =~ m/service_description/ && $bset eq 0 && $bend eq 0) { print "(L4) SD here. \n"; print MYFILE $nextline4; $bset = 1; } if ($nextline4 !~ m/service_description/ && $bend eq 0 ) { if ($bset eq 0) { print "(L4) Is not SD,and SD not found so far keep going. \n"; print MYFILE $nextline4; } if ($bset eq 1) { print MYFILE $nextline4; } } #Line 5 etc....