Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Problem with String replace in file

by philosophia (Sexton)
on Oct 22, 2007 at 17:11 UTC ( #646502=perlquestion: print w/replies, xml ) Need Help??

philosophia has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to replace || with |\N| in a text file. I'm using

open (IN, $filename);

while (<IN>){
s/||/|\\N|/;
print IN;
}

close IN;


It doesn't seem to be working. It seems to be replacing || with |N|. How can I fix this?

Replies are listed 'Best First'.
Re: Problem with String replace in file
by meraxes (Friar) on Oct 22, 2007 at 17:23 UTC

    The | is the "or" operator in a regular expression. You need to escape it for your regex to work:

    s/\|\|/|\\N|/g;
    --
    meraxes
      if I use

      s/\|\|/|\\N|/g;

      it replaces || with ||. The \N seems to be an invisible newline character, and I want it to just be '\N'.

        I suspect your problem is that you are writing back to the filehandle IN when you (presumably) only opened it for read.

        open( IN, '<', $filename ); # input open( OUT, '>', 'newfilename.txt' ); # output while ( <IN> ) { s/\|\|/|\\N|/g; print OUT; } close IN; close OUT;
        --
        meraxes

        \n will result in a newline.
        \N will result in a syntax error.
        \\N will give \N.

        If you're not getting \N from \\N, there's something you haven't showed us.

        Update: meraxes caught the problem.

Re: Problem with String replace in file
by Fletch (Bishop) on Oct 22, 2007 at 17:23 UTC

    Because | is a special character to regular expressions (which the left hand side of a substitution is). Read quotemeta (and probably perlretut and perlre as they also cover this).

Re: Problem with String replace in file
by ikegami (Patriarch) on Oct 22, 2007 at 17:23 UTC

    | is a special character for regexs. It needs to be escaped.

    s/\|\|/|\\N|/;

    However, that solution is far from complete.
    What if the first field is empty?
    What if the last field is empty?
    What if there's more than one empty fields?
    What if there's more than one empty fields in a row?
    Fix:

    s/\|(?=\|)/|\\N/g; s/^\|/\\N|/; s/\|$/|\\N/;

    As a one-liner:

    perl -pe"s/\|(?=\|)/|\\N/g; s/^\|/\\N|/; s/\|$/|\\N/" infile >outfile
      if I use

      s/\|(?=\|)/|\\N/g; s/^\|/\\N|/; s/\|$/|\\N/g;

      it replaces || with ||. The \N seems to be an invisible newline character, and I want it to just be '\N'.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://646502]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2022-05-29 06:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (101 votes). Check out past polls.

    Notices?