LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-19-2010, 10:19 AM   #1
shashv2
LQ Newbie
 
Registered: Jul 2010
Posts: 5

Rep: Reputation: 0
sed


I am trying to replace the default path in a file(which is a script)
with the new path which I have get when the bash script runs

The code is

SCRIPT=$(readlink -f $0)
SCRIPTPATH=`dirname $SCRIPT`
sed -i "s/$HomeDir/$SCRIPTPATH/g" <filename>

where $HomeDir=/default/path

I get the following error
sed: -e expression #1, char 8: unknown option to `s'

I have tried various alternates mentioned on the forum but nothing works. I know the backslash in the path name needs to be escaped but I obtain the SCRIPTPATH as a variable during runtime.

Is there a way out?
Any help will be much appreciated.
Thanks,
 
Old 07-19-2010, 10:56 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Both variables have forward slashes in them, which conflict with the forward slashes that sed uses by default. You can change the separators to just about anything you want. Try this: sed -i "s%$HomeDir%$SCRIPTPATH%g" <filename>

And even better would be this:

sed -i "s%${HomeDir}%${SCRIPTPATH}%g" <filename>

Hope this helps.
 
1 members found this post helpful.
Old 07-19-2010, 02:01 PM   #3
shashv2
LQ Newbie
 
Registered: Jul 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks! It works.
 
Old 07-19-2010, 02:07 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
You're welcome
 
Old 07-19-2010, 07:26 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Please mark as SOLVED once you have a solution.
 
0 members found this post helpful.
  


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
sed - use sed to replace multiple instances from "x" to "y" in a line mrodmac Linux - General 4 02-02-2010 11:37 AM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:36 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
Open Source Consulting | Domain Registration