in reply to changing data in a file

It *sounds* like you're wrting out to a text file then reading it back in again line-by-line. Obviously, if you save out a CR/LF in the middle of some data, this will constitute an end-of-line marker for your 'read' script, splitting your line into two.

Therefore, you probably need to 'escape' \r and \n and then unescape them when you read them back. A common trick, if you are using HTML to display your data, is using something like s/[\r\n]/<br>/g - this will replace all CR/LF with <br> tags. If you want them displayed back as carriage returns, then your 'read' script simply reverses the transformation.

Another common way is to replace with the *string* "\n" (s/\n\/\\n/g;) which again you transform back (s/\\n/\n/g;) when you read it in.

Cheers,
Ben.

Replies are listed 'Best First'.
Re^2: changing data in a file (pitfalls of proper quoting)
by Aristotle (Chancellor) on Apr 21, 2003 at 03:20 UTC
    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:

    s/\\/\\s/g; s/\n/\\n/g; s/\r/\\r/g;
    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/\\n/\n/g; s/\\r/\r/g; s/\\s/\\/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:
    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:
    good interfaces and user interfaces.

    — Dan J. Bernstein, The qmail security guarantee

    Makeshifts last the longest.

Re: Re: changing data in a file
by simonthom (Initiate) on Apr 20, 2003 at 19:04 UTC
    Ben, Thanks for the great help. You understood my problem and I took your advice of substituting a <br> for the CR/LF and have the script working. Many thanks, Simon

    2003-04-20 edit ybiC: HTML encoding for <br>

      My pleasure - and welcome to the monastery, btw. A couple of minor points that might also help - you need to use &lt; and &gt; in order to display an html tag, like that <br> there...and don't fret about Abigail-II's 'express yourself properly boy!' answer - you got off lightly. :) Do have a wander around the halls though and check out the PerlMonks FAQ section on effective questioning - the hardest thing to do here is have to parse a question before being able to understand / answer it :).

      cheers
      Ben