in reply to how to remove a specific character from a file efficiently..

The /g is silly since it can't possibly find more than one match.

The regex engine doesn't realize the match has to be at the end of the string, so it might help to only look there if the lines are really long.

substr($line, -6, 6, "\")]]]") if substr($line, -6) eq "\n\")]]]";

Not sure how it will affect you.

But if your lines are so long that the regex match is slowing you down, perhaps you shouldn't make a needless copy of it. Get rid of my $line=$_;. Either work with $_, or use while (my $line = <AH>) to assign to $line instead of $_.