Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

[solved] Replace ".." with ","

by zaqwsxcde (Initiate)
on Apr 05, 2015 at 14:14 UTC ( [id://1122481]=perlquestion: print w/replies, xml ) Need Help??

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

Hello,

I want to replace the separator ".." (two points) by a comma. I have tried with no success the following commands:
perl -pe 's/../,/' input.txt >output.txt perl -pe 's/".."/,/' input.txt >output.txt perl -pe 's/'..'/,/' input.txt >output.txt perl -pe 's/t./,/' input.txt >output.txt

Here is an example of the text which I am editing:

1234456..123456 ytrv iuuhygjk 123466..9876

I want it to be changed to this:

1234456,123456 ytrv iuuhygjk 123466,9876

Please advice what is wrong with the oneliners above.

Thanks!

Replies are listed 'Best First'.
Re: Replace ".." with ","
by choroba (Cardinal) on Apr 05, 2015 at 14:21 UTC
    Dot has a special meaning in regular expressions - it matches anything (except for a newline). To match a dot literally, use [.] or \..

    Also, use the /g modifier to replace all the occurrences on each line.

    s/\.\./,/g # or s/[.][.]/,/g
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Replace ".." with ","
by afoken (Chancellor) on Apr 05, 2015 at 14:22 UTC

    "." needs to be escaped in regular expressions. Use s/\.{2}/,/g or s/[.]{2}/,/g. The former uses an escaped ".", the latter a character class containing only the "."

    Alexander

    Updated: First variant should be s/\.{2}/,/g, not s/\.{2}//,/g. Thanks to AnomalousMonk for spotting this bug.

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Replace ".." with ","
by aaron_baugher (Curate) on Apr 05, 2015 at 14:22 UTC

    In a pattern match, the period means "any character". You need to escape it to make it match only a literal period. One way:

    s/\.\./,/;

    Aaron B.
    Available for small or large Perl jobs and *nix system administration; see my home node.

Re: Replace ".." with ","
by zaqwsxcde (Initiate) on Apr 05, 2015 at 14:28 UTC
    Great, the \.\. idea works. Thank you all!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-24 16:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found