replace text in file using variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace text in file using variable
# 1  
Old 01-18-2011
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 :
Code:
sed -i 's/BOOTPATH/TEST/g' file.xml

it works fine

but if I use a viriable :
Code:
sed -i 's/BOOTPATH/$bootpathf/g' file.xml

with this one, no change are made.

I don't understand why. If a make a
Code:
echo $bootpathf

the displayed value is correct.
Smilie

Last edited by Scott; 01-18-2011 at 01:57 PM..
# 2  
Old 01-18-2011
Have you tried to use " instead of ' to surround your command?
# 3  
Old 01-18-2011
Sorry I don't understand the 'instead of'. I am a newby.
# 4  
Old 01-18-2011
try
sed -i "s/BOOTPATH/$bootpathf/g" file.xml
# 5  
Old 01-18-2011
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 ----------

Quote:
Originally Posted by Toug
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.
Smilie
---------- 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.
# 6  
Old 01-18-2011
You might be using single quotes. Make sure you use double quotes.
Code:
sed -i "s/BOOTPATH/$bootpathf/g" file.xml

If no good, post your command.
# 7  
Old 01-18-2011
Your previous note:
Quote:
sed: -e expression #1, char 13: unknown option to `s'
leads me to think you are not using the " character, next to the ENTER key

Please post your command.
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