Ok, it doesn't remove that line and if the $line isn't there. It put the $bgs here doc there. But, if it has the wrong $line. if($line =~ m/$agent/){ $line = $collect; It fixes it and also adds the $bgs here. same with if the $line is right. It doesn't remove the line anymore. But, it adds the $bgs here to that file too. Thanks!