Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 02-26-2010, 02:41 AM   #1
LQ Newbie
Registered: Jan 2010
Location: india
Distribution: fedora core 12
Posts: 2

Rep: Reputation: 0
replace a string in a file using a script with path containig /

Hi all,

i need to replace a string in a file( using a script(

contents of
mount -t nfs -o nolock` /nfs
contents of

after running should be filled with nfs path like /home/vimal etc
but im getting error since path contains /. how to remove this


Thanks and Regards,

Old 02-26-2010, 02:46 AM   #2
LQ Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 553Reputation: 553Reputation: 553Reputation: 553Reputation: 553Reputation: 553
Two Three tips that may help:

1) Put your sed command within double-quotes, like: sed "s/blah/blah"


2) you probably do not really want to be using that echo statement within backticks; why not simply put the variable itself in there?


3) Since there are slashes within the path (the variable) you will need to use a delimiter in your sed statements that is NOT a slash; consider maybe a % sign, or a pipe | symbol.

Old 02-26-2010, 05:18 AM   #3
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 404Reputation: 404Reputation: 404Reputation: 404Reputation: 404
If you use bash you could even do this:

$ foo="/old/uncle/doe"
$ echo "${foo/uncle/uncle/john/moo/cow}"
Sasha's solution is more portable, and is the one you should be using if you put #!/bin/sh in the header of your script. You can use almost anything in place of '/' in sed, that gives you quite a lot of freedom when dealing with strange strings.
Old 02-27-2010, 01:15 AM   #4
LQ Newbie
Registered: Jan 2010
Location: india
Distribution: fedora core 12
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks sasha nd all.
I tried % as delimitter and it worked (sasha's tip no 3). i tried tip no 1, it was giving some error.
Thanks one more time. your quick reply helped me to incorporate that script in release.


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
Shell scripting - Getting just directory string from file path string? arashi256 Programming 5 10-16-2009 09:21 AM
bash script path issue - how to pass a path as a string to a variable PiNPOiNT Programming 5 04-17-2009 06:48 PM
Please help me replace a string within a file struct Programming 7 03-04-2009 10:05 AM
How do I replace special characters in a string within a bash script? rhaup0317 Linux - Newbie 2 06-03-2008 12:56 PM
Shell script: Find "\n\t..." to replace a string in a file michael24h7d Programming 8 05-11-2007 04:07 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:04 PM.

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