Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Replace text - includes tilde sign?

by bobafifi (Beadle)
on Sep 21, 2007 at 11:11 UTC ( #640331=perlquestion: print w/replies, xml ) Need Help??

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

Can somebody please tell me the syntax to do a find/replace command that includes a tilde sign as part of the string output? For example:
perl -i -pe 's/example\.com/12\.34\.56\.78/~example/g' *

getting compilation errors -- desired output of the above (to replace example.com):
12.34.56.78/~example


Thanks!
-Bob
bobafifi.com

Replies are listed 'Best First'.
Re: Replace text - includes tilde sign?
by Corion (Pope) on Sep 21, 2007 at 11:20 UTC

    Is the tilde part of the problem?

    The reason I'm asking this is, because it's not. It would help us tremendously to help you better if you showed the error message you got. That error message might also help you to find the error.

    Also, it often helps to describe what you want a particular snippet to do. In your specific case, it seems as if you want to replace all strings "example.com" to be replaced by "12.34.56.78/~example". Note that this will also affect strings like http://www.badexample.com which will become http://bad12.34.56.78/~example if your replacement worked.

      Got it - yikes. Forgot to backslash the forward slash before the tilde sign! sorry...

      Thanks for all the replies nonetheless!

      -Bob
      bobafifi.com
Re: Replace text - includes tilde sign?
by dwm042 (Priest) on Sep 21, 2007 at 11:39 UTC
    As far as I can see, the problem isn't the tilde so much as the unescaped character just before the tilde. It's the same as your separator. If you use a slightly different syntax for the search and replace, you can do the transformation without issue.

    ~/perl/monks$ cat test_tilde.pl #!/usr/bin/perl use warnings; use strict; my $test = "This is a test of a replacement string, really (example.com) and truly."; print $test, "\n"; $test =~ s|example\.com|12\.23\.56\.78\/\~example|g; print $test, "\n";
    The output is:

    ~/perl/monks$ ./test_tilde.pl This is a test of a replacement string, really (example.com) and truly. This is a test of a replacement string, really (12.23.56.78/~example) and truly.
Re: Replace text - includes tilde sign?
by johngg (Canon) on Sep 21, 2007 at 11:23 UTC
    Try a different delimiter. You are using a slash but your replacement text also has a slash in it.

    Cheers,

    JohnGG

Re: Replace text - includes tilde sign?
by gube (Parson) on Sep 21, 2007 at 11:29 UTC
    Try this it will work
    perl -i -pe 's#example.com/12.34.56.78#~example#g' *
    Gubs

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2020-07-10 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?