Monday, December 16, 2013

How to replace a string (directory structure) in a file

www.unixbabuforum.inI have one file in which I have 1 variable set. 

bash-3.00$ more dev19/testfile 
location=/opt/www/dev19/portal/domain.www.$in_ name/WWWIdentity.jks 

I have written 1 script where I am replacing this variable value 

bash-3.00$ more test_sk 
#!/bin/ksh 
dt=`date +"%h%d%y"` 
echo "Enter Instance name : eg dev19 :- \c" 
read in_name 
DIR1=/users/apps/isd/snegi/$in_name 
FILE1=testfile 
echo "Taking backup of files....... \n" 
cp $DIR1/$FILE1 $DIR1/$FILE1_$dt 
echo "Now changing the files......\n" 
iloc="/opt/www/$in_name/portal/domain.www.$in_ name/WWWIdentity.jks" 
new_www=/opt/www/certs/WWWIdentity.jks 
perl -i -p -e "s/$iloc/$new_key/g" $DIR1/$FILE1 

But I am getting the error hile running this script 

bash-3.00$ ./test_sk 
Enter Instance name : eg dev19 :- dev19 
Taking backup of files....... 
Now changing the files...... 
Bareword found where operator expected at -e line 1, near "s//opt/www" 
syntax error at -e line 1, near "s//opt/www" 
Search pattern not terminated at -e line 1. 

 How to replace a directory structure in a file

www.unixbabuforum.inYou must change the delimiter 
perl -i -p -e "s-$iloc-$new_key-g" $DIR1/$FILE1


www.unixbabuforum.inLooking at perl statement, I guess you are using different variable $new_key. It should be defined. perl statement should be something like this... 
perl -i -p -e 's/$iloc/$new_www/g' $DIR1/$FILE1 

0 comments:

Post a Comment

 
Design by BABU | Dedicated to grandfather | welcome to BABU-UNIX-FORUM