LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-06-2010, 08:46 AM   #1
Hari_Keane
LQ Newbie
 
Registered: Apr 2010
Posts: 4

Rep: Reputation: 0
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.
 
Old 04-06-2010, 09:04 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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
 
Old 04-06-2010, 09:36 AM   #3
Hari_Keane
LQ Newbie
 
Registered: Apr 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Thumbs up

Thks it worked perfect.
 
Old 04-06-2010, 09:37 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,423

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Remember to mark as SOLVED when solution has worked.
 
Old 04-06-2010, 09:45 AM   #5
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to stop bash from replacing "~/" with "/home/username"? daihard Linux - Software 8 08-22-2010 03:32 PM
Trivial regular expression for replacing "\n.\n" with "\n .\n" gregorian Linux - Newbie 3 08-23-2009 05:40 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 11:18 PM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 09:31 AM
Replacing "function(x)" with "x" using sed/awk/smth Griffon26 Linux - General 3 11-22-2006 11:47 AM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration