Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Problem with String replace in file

by philosophia (Sexton)
on Oct 22, 2007 at 17:11 UTC ( [id://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?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-25 23:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found