while (my $line=) { chomp $line; $line =~ s/\s+\|/\|/g; # note the "g" on the end. printf "\"%s\"\n",$line; } __END__ B-52 |P-51 | P-61 P-48 |B-1B | JU-88 B-29 |ME-109 #### "B-52|P-51| P-61" "P-48|B-1B| JU-88" "B-29|ME-109"