replace text in file using variable

Hi,
I'm making a script that automaticaly set file size and path in xml file.

I tried with :

sed -i 's/BOOTPATH/TEST/g' file.xml

it works fine

but if I use a viriable :

sed -i 's/BOOTPATH/$bootpathf/g' file.xml

with this one, no change are made.

I don't understand why. If a make a

echo $bootpathf

the displayed value is correct.
:wall:

Have you tried to use " instead of ' to surround your command?

Sorry I don't understand the 'instead of'. I am a newby.

try
sed -i "s/BOOTPATH/$bootpathf/g" file.xml

it return the error:
sed: -e expression #1, char 13: unknown option to `s'

---------- Post updated at 05:03 PM ---------- Previous update was at 05:01 PM ----------

---------- Post updated at 05:06 PM ---------- Previous update was at 05:03 PM ----------

I was wrong with the '
a modification is made, but it put "$bootpathf" instead of the variable value.

You might be using single quotes. Make sure you use double quotes.

sed -i "s/BOOTPATH/$bootpathf/g" file.xml

If no good, post your command.

Your previous note:

leads me to think you are not using the " character, next to the ENTER key

Please post your command.

sed -i "s/BOOTPATH/$bootpathf/g" $rootfspath.xml

return :

sed: -e expression #1, char 13: unknown option to `s'

I attached the full script.

Can you cut a few lines out of your script, showing where you set the variable and where you execute the sed command? (Rather than trying to read thru your entire zipped script.)

Not very sure but looks like $bootpathf has "/" in it's value as it is a path.
e.g. abc/def/ghi
If so, use following:

sed -i "s#BOOTPATH#$bootpathf#g" $rootfspath.xml
 

Replace / in sed command with some other character which is not part of values.

reply for joeyg:

here is a summary:

bootsize=$(ls -l *boot.bin | awk '{printf "%10d %s\n",$5,$9}')  #this line return the size of a file named XXXXXboot.bin and put it in the bootsize variable(only one file in the current folder)
let bootsize=bootsize/512                                        #this line return the number of 512 bytes block of bootsize
let bootsize=bootsize+1                    
folderpath=$(pwd)                                                # return the current folder in use in the folderpath variable
bootpath=$(ls -a *boot.bin)                                        # return the name of the XXXXboot.bin
bootpathf=$folderpath/$bootpath                                    # return the full path of the XXXXXboot.bin
cp ../../TEMPLATE_OS_STITCH_penwellB0.xml $rootfspath.xml        # copy a config template file to be modified into the current folder and name it with another file (+ .xml)
sed -i "s/BOOTPATH/$bootpathf/g" $rootfspath.xml                # modify the file to replace the pattern BOOTPATH by the XXXXboot.bin full path

I hope this will help to understand.

You have / characters in your variable. Thus, the sed program having problems with your command. Use a different delimiter in your sed command.

post# 10 will work.

Thanks for all, it works fine using # and ".