replace text in file using variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace text in file using variable
# 8  
Old 01-18-2011
sed -i "s/BOOTPATH/$bootpathf/g" $rootfspath.xml

return :

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


I attached the full script.
# 9  
Old 01-18-2011
Cut a few lines out of your 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.)
# 10  
Old 01-18-2011
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:
Code:
sed -i "s#BOOTPATH#$bootpathf#g" $rootfspath.xml

Replace / in sed command with some other character which is not part of values.
# 11  
Old 01-18-2011
reply for joeyg:

here is a summary:

Code:
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.
# 12  
Old 01-18-2011
See previous post by anurag

You have / characters in your variable. Thus, the sed program having problems with your command. Use a different delimiter in your sed command.
# 13  
Old 01-18-2011
post# 10 will work.
# 14  
Old 01-19-2011
Thanks for all, it works fine using # and ".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to replace a parameter(variable) date value inside a text files daily with current date?

Hello All, we what we call a parameter file (.txt) where my application read dynamic values when the job is triggered, one of such values are below: abc.txt ------------------ line1 line2 line3 $$EDWS_DATE_INSERT=08-27-2019 line4 $$EDWS_PREV_DATE_INSERT=08-26-2019 I am trying to... (1 Reply)
Discussion started by: pradeepp
1 Replies

2. UNIX for Dummies Questions & Answers

Replace variable string with text

Hi All, Hoping someone can help.... I am trying to work out how I can ammend a log file to remove variable strings in order to remove confidential information which I cant pass on. As an example I have used phone numbers. A large log file contains multiple lines containing something like the... (6 Replies)
Discussion started by: mutley2202
6 Replies

3. Windows & DOS: Issues & Discussions

2 Questions: replace text in txt file, add text to end of txt file

so... Lets assume I have a text file. The text file contains multiple "#" symbols. I want to replace all thos "#"s with a STRING using DOS/Batch I want to add a certain TEXT to the end of each line. How can I do this WITHOUT aid of sed, grep or anything linux related ? (1 Reply)
Discussion started by: pasc
1 Replies

4. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

5. Shell Programming and Scripting

Replace variable names in text file with its value

Hi I have a text file (mytext.txt), the content of which is as following: My name is <@MY_NAME@> and my age is <@MY_AGE@> . Now i have another property file (myprops) which is as following: MY_NAME=abcdefgh MY_AGE=000000 I was wondering, how can i replace the tags of mytext.txt, with its... (1 Reply)
Discussion started by: vigithvg
1 Replies

6. Shell Programming and Scripting

How to replace text in a file with text entered

I am trying to write a shell script that will allow the typing of a value, then using that value to replace data in a text file. I suspect I need sed. The format of the file is: Variable1:Value1 Variable2:Value2 The interaction would be something like: Shell Prompt: "Please enter the... (9 Replies)
Discussion started by: cleanden
9 Replies

7. UNIX for Dummies Questions & Answers

search and replace a specific text in text file?

I have a text file with following content (3 lines) filename : output.txt first line:12/12/2008 second line:12/12/2008 third line:Y I would like to know how we can replace 'Y' with 'N' in the 3rd line keeping 1st and 2nd lines same as what it was before. I tried using cat output.txt... (4 Replies)
Discussion started by: santosham
4 Replies

8. UNIX for Dummies Questions & Answers

sed to replace text with a variable

I'm trying to replace text in a file with text from a variable I have the following in my script, but its not working: #!/bin/ksh echo "Enter the path to load scripts" read x echo "updating the templates" sed "s/CHANGE_ME_TO_LOAD_PATH/"$x"/g" LoadFiles.sh > LoadFiles2.sh I thought... (1 Reply)
Discussion started by: orahi001
1 Replies

9. Shell Programming and Scripting

Sed , Replace a "variable text" inside of a statement

Please Help... I am trying to manipulte the following line Before : <user:Account_Password>002786</user:Account_Password> the password is the "variable", i need to delete / omit the password in the file, (it occurs several thousand times) so the tag line looks like After:... (4 Replies)
Discussion started by: jackn7
4 Replies

10. Shell Programming and Scripting

find and replace text with a variable?

Is there a way that I can make the following work with using variables? perl -pi -e 's#blah#hrm#ig' replacetext but like this var=blah perl -pi -e 's#$var#hrm#ig' replacetext (3 Replies)
Discussion started by: doublejz
3 Replies
Login or Register to Ask a Question