sed to replace text with a variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sed to replace text with a variable
# 1  
Old 03-06-2008
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 using double quotes instead of single quotes would allow me to substitue a variable with sed.

Can someone explain what I'm doing wrong here?

thanks
# 2  
Old 03-06-2008
I resolved my problem with replacing the "/" with "|"

sed -e "s|CHANGE_ME_TO_LOAD_PATH|$x|g" LoadFiles.sh > LoadFiles2.sh

sed wasn't working because I was supplying the "/" at for the begining of my variable.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Sed/replace using variable

Hello everyone, In one of my shell script I am doing sed/replace using a variable to find a string in a file & replace it with another string in same file. The challenge which I am facing is that, the variable which I am using with "sed" is having one of character as "/" Here is below how... (4 Replies)
Discussion started by: gr8_usk
4 Replies

3. Shell Programming and Scripting

find and replace with variable -sed

Hi, I have a ksh script where I am trying to mask the password in the log files. $loc - is my directory $PGUIDE_DB_USER_PSW - is a variable that holds the password I am looking for find $loc/logs -type f -exec sed -i "s/$PGUIDE_DB_USER_PSW/*****/"g {} \; I get an error: ... (2 Replies)
Discussion started by: amitlib
2 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 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 ... (13 Replies)
Discussion started by: Toug
13 Replies

6. Shell Programming and Scripting

replace text with SED

Hi, I hope someone can help me out with the following: I have a file with the following lines in it: something /path/dir/my_-_file.01.ext sometext sometext somethingelse /path/dir/my_-_file.02.ext sometext something /path/dir/my_-_file.03.ext sometext some other text And i want to... (3 Replies)
Discussion started by: thyssimonis
3 Replies

7. Shell Programming and Scripting

Replace with a variable in sed command

Hello, I have this command and it works fine. My question is that how can we replace the N by a variable, to print for instance a big number of lines. It means if I want 100 lines after an expression, to not put "N" 100 times in the sed. Code: $ sed -n '/aaa/{n;N;N;s///g;s/;/; /g;p;}'... (2 Replies)
Discussion started by: rany1
2 Replies

8. 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

9. Shell Programming and Scripting

replace with value of variable using SED

Hello everyone, I need to replace a pattern with "value of a variable" using sed but no luck. i tried the below ABC=STR2 sed 's/STR1/$ABC/g' <file> (also tried \ , $ and " in combination but failed) Appreciate any help. Thanks Prvn (2 Replies)
Discussion started by: prvnrk
2 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