in reply to Re: changing data in a file
in thread changing data in a file
s/\\n/\n/g;
You've broken the solution by creating it: I might have had a plaintext \n in the text before, that now becomes a newline. Oops.
If you escape any characters, you need to escape the escape character as well:
so that a plaintext \n becomes \sn, which won't be matched by the regex that unescapes newlines. Then you can do the same thing backwards:s/\\/\\s/g; s/\n/\\n/g; s/\r/\\r/g;
Just don't forget to leave the last unescaping last. If the escape code for the character is itself (ie s/\\/\\\\/g;), then unescaping becomes much more complicated, because you could match a \n that's actually part of a sequence like \\n. So you'll have to unwrap the string very carefully:s/\\n/\n/g; s/\\r/\r/g; s/\\s/\\/g;
my %xlat = ('\\' => "\\", 'n' => "\n", 'r' => "\r"); s/\G(?:\\(.)|(.))/defined($1) ? $xlat{$1} : $2/eg;
And I haven't tested this, so I'm not even sure I got it right on the first try.
Quoting and escaping are hair-raisingly complex issues - be careful.
I have discovered that there are two types of command interfaces in the world of computing:
— Dan J. Bernstein, The qmail security guarantee
good interfaces and user interfaces.
Makeshifts last the longest.
|
|---|