The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
rsh to change multiple ip in multiple servers? kenshinhimura Shell Programming and Scripting 2 02-17-2008 11:04 PM
About NIMs Mustur007 AIX 1 08-14-2007 01:06 PM
Career Path/Change - Cert Help CoopDeVille What's on Your Mind? 2 06-15-2007 05:41 PM
Change Default Path ? tkbharani UNIX for Advanced & Expert Users 4 01-13-2007 11:53 PM
How to loop thru and change multiple files in realtime system? merliech Shell Programming and Scripting 2 04-24-2006 09:25 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-22-2007
Registered User
 

Join Date: Mar 2007
Posts: 8
to change a path /appl/nims with an /appl2/nimsrobt in multiple files using sed

Hi All,

I have developed a script in which i have taken the path from the user in a variables named FromPath and ToPath.

I want to replace the FromPath with ToPath. For that I am using sed command in the following way.

cp $file bk_$file

FromPath=/appl/nims/nimsc1

FromPath=`echo $FromPath | sed 's/\//\\//g'

It should return \/appl\/nims\/nimsc1 to use it in SED command.

sed 's/$FromPath/$ToPath/g' bk_$file > $file


But it is not working and it is raising an error command garbled.

It is working fine on the unix prompt. but in script it is raising an error.

So please co-operate me and tell me what wrong I am doing.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-22-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,475
something like this

Code:
echo $FromPath| sed 's/\//\\\//g'
Reply With Quote
  #3 (permalink)  
Old 03-22-2007
Registered User
 

Join Date: Mar 2007
Posts: 8
Thank you for you reply but i am gettinng an error.

I have already tried the script you have given. the error is sed: command garbled: s/\//\\//g. It is working on the unix prompt but not in script.

My some portion of the code is as below

FromPath=`echo $FromPath | sed 's/\//\\\//g'`
ToPath=`echo $ToPath | sed 's/\//\\\//g'`
From_UIDPWD=`echo $From_UIDPWD | sed 's/\//\\\//g'`
To_UIDPWD=`echo $To_UIDPWD | sed 's/\//\\\//g'`

echo $From_UIDPWD $To_UIDPWD $FromPath $ToPath $From_ORA_SID

$To_ORA_SID
cd scripts
for file in my_*.sh
do
echo $file
if [ -w $file ]
then
cp $file bk_$file
#sed 's/$From_UIDPWD/$To_UIDPWD/g;s/$FromPath/$ToPath/g;s/@$From_ORA_SID/@$To_ORA_SID/g' bk_$file > $file
sed 's/@$From_ORA_SID/@$To_ORA_SID/g' bk_$file > $file
rm bk_$file
else
echo "The file $file is read only file." | tee -a CouldNotChangeFile.log
fi
done

Suggest me something to solve this problem.
Reply With Quote
  #4 (permalink)  
Old 03-22-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
This is not needed.
Code:
FromPath=`echo $FromPath | sed 's/\//\\//g'

It should return \/appl\/nims\/nimsc1 to use it in SED command.
Use double quotes to get the value of variables inside sed command
Code:
sed "s;$FromPath;$ToPath;g" bk_$file > $file
Reply With Quote
  #5 (permalink)  
Old 03-26-2007
Registered User
 

Join Date: Mar 2007
Posts: 8
Thank you it is working fine now.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:47 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0