LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl: replace character in file (https://www.linuxquestions.org/questions/programming-9/perl-replace-character-in-file-539862/)

vippie 03-23-2007 03:23 AM

Perl: replace character in file
 
Hello,

I've read all the topics on this problem, the solution works, but not for my specific problem.

I want to replace a dot by a comma in a .txt file

This should be possible by doing this on the command line:

/usr/bin/perl -p -i.bak -e "s/./,/g" *.txt

This returns the *.txt file full with comma's (and a .bak backup file), it replaced every character with a comma.

If I do this:

/usr/bin/perl -p -i.bak -e "s/i/,/g" *.txt

then it works fine and it replaces every 'i' with a comma.

So it seems it does work with every character, except dots
:(

It might be a noobisch question, but can somebody help me to fix this?

kshkid 03-23-2007 03:24 AM

this would work,

Code:

s/\./,/g

vippie 03-23-2007 03:26 AM

thanks for the fast reply, it works

(this is my first day programming perl)

son_t 03-23-2007 03:37 AM

Quote:

Originally Posted by vippie
thanks for the fast reply, it works

(this is my first day programming perl)

But do you understand? The '.' means match any character, so if you want to match the literal '.', you have to escape it with a \.

vippie 03-23-2007 04:26 AM

Yes, I understand now, figured that . means every character. Didn't know about the '\' escape


All times are GMT -5. The time now is 01:00 AM.