Skip to content

Instantly share code, notes, and snippets.

@Akronix
Created December 4, 2018 10:24
Show Gist options
  • Save Akronix/d2afaf1bef252debdaf3bfc49df87b83 to your computer and use it in GitHub Desktop.
Save Akronix/d2afaf1bef252debdaf3bfc49df87b83 to your computer and use it in GitHub Desktop.
To fix the issue of having double quotes inside attribute values in XML files. The script substitutes every double quote (") by its corresponding value: \" inside XML attributes, as long as it isn't the opening or closing quote after the attribute name.
use utf8;
use feature 'say';
#~ $filename=$ARGV[0];
# uncomment to define the input file here instead of using standard input
#~ my $encoding = ":encoding(UTF-8)";
#~ open (FH, " < $encoding", $filename) or die "Error trying to write on $filename: $!\n";
# uncomment to define the output file here instead of using standard output
#~ $aux_fn = "out2";
#~ open (OUT, " > $encoding", $aux_fn) or die "Error trying to write on $aux_fn: $!\n";
foreach $line (<STDIN>) {
my ($prev, $value, $after) = $line =~ '(.*=")(.*)(" .*)';
#~ say $prev;
#~ say $value;
#~ say $after;
if (not $prev) {
print STDOUT "$line";
next;
}
#~ say $value;
$value =~ s/\"/\&quot;/g;
#~ say $value;
print STDOUT "$prev$value$after\n";
#~ print "$line\n";
}
#~ close(FH);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment