1,105,406 Community Members

Perl: how to replace character '/' with '\'

Member Avatar
alan_123
Newbie Poster
3 posts since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I am new to perl.
In a string How do I replace a backward slash with a forward slash?
when I call the following perl api $FindBin::Bin; I get the path as follows
$myPath = c:/perl/test
I would like to covert this to
c:\perl\test
I did try the following and get errors
#$myPath=~ tr/\//\/c;

Member Avatar
d5e5
Practically a Posting Shark
827 posts since Sep 2009
Reputation Points: 109 [?]
Q&As Helped to Solve: 163 [?]
Skill Endorsements: 2 [?]
 
0
 
#!/usr/bin/perl -w
#ReplaceBackslashWithSlash.pl
use strict;

my $myPath = 'c:\perl\test'; # Example string containing backslashes
print "\nOriginal string is: $myPath\n\n";

# The following command substitutes backslashes with forward slashes
# You need to escape the backslash and slash with backslash
$myPath =~ s/\\/\//g;
print "Modified string is: $myPath\n";
Member Avatar
alan_123
Newbie Poster
3 posts since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks for the replay,
Sorry I made a mistake. the source string is
'c:/perl/test'
would like to convert as
c:\perl\test

Member Avatar
d5e5
Practically a Posting Shark
827 posts since Sep 2009
Reputation Points: 109 [?]
Q&As Helped to Solve: 163 [?]
Skill Endorsements: 2 [?]
 
0
 

Sorry, I read your example wrong. This should do it. Too bad the substitute command uses slashes to delimit the two strings. It confuses me, but it should work this time.

#!/usr/bin/perl -w
#ReplaceSlashWithBackslash.pl
use strict;

my $myPath = 'c:/perl/test'; # Example string containing backslashes
print "\nOriginal string is: $myPath\n\n";

# The following command replaces forward slashes with backslashes
# You need to escape the backslash and slash with backslashes
$myPath =~ s/\//\\/g; #There must be a better way, but I don't know it.
print "Modified string is: $myPath\n";
Member Avatar
rocky martin
Newbie Poster
1 post since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 
#!/usr/bin/perl -w
#ReplaceBackslashWithSlash.pl
use strict;

my $myPath = 'c:\perl\test'; # Example string containing backslashes
print "\nOriginal string is: $myPath\n\n";

# The following command substitutes backslashes with forward slashes
# You need to escape the backslash and slash with backslash
$myPath =~ s/\\/\//g;
print "Modified string is: $myPath\n";

why have u not write in 4th and 7th line

Member Avatar
d5e5
Practically a Posting Shark
827 posts since Sep 2009
Reputation Points: 109 [?]
Q&As Helped to Solve: 163 [?]
Skill Endorsements: 2 [?]
 
0
 

Blank lines won't make any difference to how the program runs. Since I'm new to Perl I probably don't have a good sense of style yet. But I was trying to separate my code into three sections: introductory lines that appear in all programs, followed by setting up and printing the test string, followed by modifying and printing the resulting string.

Member Avatar
alan_123
Newbie Poster
3 posts since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you all for your support

Question Answered as of 4 Years Ago by d5e5 and rocky martin
Member Avatar
maaria
Newbie Poster
2 posts since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

no i have no any idea about this. i am thankful to you for this topic. i like it very much.

Member Avatar
dozierc
Newbie Poster
2 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hey guys I'm stuck. I have been trying to write a sed or perl script to change the below. But each time I run the sed script or perl it completely zeros out the file.

I want to change this
/opt/dragon/bin/runit

to this
cd /opt/dragon/bin; /opt/dragon/bin/runit

Member Avatar
d5e5
Practically a Posting Shark
827 posts since Sep 2009
Reputation Points: 109 [?]
Q&As Helped to Solve: 163 [?]
Skill Endorsements: 2 [?]
 
0
 

Welcome to Daniweb. Please start a new thread to ask your question. When you ask your question in a new thread, it will help if you list the script that you tried. Then maybe someone can advise you why your script didn't give you the result you wanted.

Member Avatar
mitesh.24jain
Newbie Poster
23 posts since Apr 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

string abc=c:/mit/sss/ss/;
string changingpath=abc.tostring().Replace(@"/",@"\");

label.text=changingpath.tostring();

it will sure work

Member Avatar
mitesh.24jain
Newbie Poster
23 posts since Apr 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

BUT how to replace "'" to """.
i mean single coats to double coats???

string abc= "hi my name is 'MITESH'";

I WANT THIS STATEMENT AS

hi my name is "MITESH" PLZ HELP ME

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article