LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 02-26-2010, 01:41 AM   #1
vimalraj
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(startup.sh) using a script(parser.sh).

contents of startup.sh
mount -t nfs -o nolock 192.168.44.46:NFS_SERVER_LOCATION` /nfs
contents of startup.sh
sed -i s/NFS_SERVER_LOCATION/`echo $NFS_LOCATION`/ ./startup.sh

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

--

Thanks and Regards,

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

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

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

and

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

and

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.

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

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

Code:
$ foo="/old/uncle/doe"
$ echo "${foo/uncle/uncle/john/moo/cow}"
/old/uncle/john/moo/cow/doe
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, 12:15 AM   #4
vimalraj
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.
 
  


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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
Open Source Consulting | Domain Registration