LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help Me: Trouble in Replacing "\" with "\\" (https://www.linuxquestions.org/questions/linux-newbie-8/help-me-trouble-in-replacing-%5C-with-%5C%5C-800332/)

Hari_Keane 04-06-2010 07:46 AM

Help Me: Trouble in Replacing "\" with "\\"
 
Hi All,

I am reading a file paths from a txt file and opening the files using perl.

I want to replace the path "\" in Ex: d:\work\data.txt to d:\\work\\data.txt as I am not able to open the file when the path is given with single slash.

Thanks,
Hari.

pixellany 04-06-2010 08:04 AM

You can do this in SED. Perhaps the issue is that the backslash (\) is typically the escape character and so it has to be "escaped" in typical regular expressions.

This will replace a single \ with \\:

Code:

sed 's/\\/\\\\/g' filename > newfilename

Hari_Keane 04-06-2010 08:36 AM

Thks it worked perfect.

grail 04-06-2010 08:37 AM

Remember to mark as SOLVED when solution has worked.

PMP 04-06-2010 08:45 AM

Quote:

I want to replace the path "\" in Ex: d:\work\data.txt to d:\\work\\data.txt as I am not able to open the file when the path is given with single slash.
What is your perl code? Give the snippet of the code, probably you are using double quotes with the variable.


All times are GMT -5. The time now is 07:31 PM.