while () { next unless /^~/;# Skip line unless it starts with ~ tr/_/+/; } continue { print; } #### pos($_) = -1; s/(^~ | \G) .*? \K _ /+/gx;