Using variable inside 'sed'


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using variable inside 'sed'
# 1  
Old 02-07-2007
Using variable inside 'sed'

Dear all,


How can I amend the following command to use variable inside 'sed' ?

str=`sed -e 's/orig_string/${var}/g' ${sourcefile}`


Thanks,
Rocky
# 2  
Old 02-07-2007
Use double quotes
Code:
str=`sed -e "s/orig_string/${var}/g" ${sourcefile}`

# 3  
Old 11-23-2007
Bug

Quote:
Originally Posted by anbu23
Use double quotes
Code:
str=`sed -e "s/orig_string/${var}/g" ${sourcefile}`

Also, if var is a path, then you'll need to use '\' before '/'. Sed substitutes first and then you still need to make sure you escape special characters such as '/'

Eg
set var="\/mydir\/path"
echo "my file /work/kofeyok/stuff/file.dat, r );" > testfile
sed -e "s/\/work\/kofeyok\/stuff/${var}/g" testfile > temp

temp should have
"my file /mydir/path/file.dat, r );"
without double quotes
# 4  
Old 11-23-2007
Code:
set var="/mydir/path"
echo "my file /work/kofeyok/stuff/file.dat, r );" > testfile
sed -e "s#/work/kofeyok/stuff#${var}#g" testfile > temp

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To print value for a $variable inside a $variable or file

Hi guys, I have a file "abc.dat" in below format: FILE_PATH||||$F_PATH TABLE_LIST||||a|b|c SYST_NM||||${SRC_SYST} Now I am trying to read the above file and want to print the value for above dollar variables F_PATH and SRC_SYST. The problem is it's reading the dollar variables as... (5 Replies)
Discussion started by: abcabc1103
5 Replies

2. Shell Programming and Scripting

Variable inside sed

Problem: Need to read contents from a file and use that value inside sed as avariable. sample code below. THe below code replaces contents inside file123 for matched string with "$x" value only. but we want the value of x which is read from TextFile.txt to go in there. cat TextFile.txt|while... (3 Replies)
Discussion started by: cvsanthosh
3 Replies

3. Shell Programming and Scripting

evaluating a variable inside a variable

Hi there, i think im getting myself a little confused and need some help :wall: I am reading in a bunch of variables to my script from an external file and need to validate that a value has been set for each so if you can imagine, the user is required to pass in 4 values... (3 Replies)
Discussion started by: rethink
3 Replies

4. Shell Programming and Scripting

sed inside sed for replacing string

My need is : Want to change docBase="/something/something/something" to docBase="/only/this/path/for/all/files" I have some (about 250 files)xml files. In FileOne it contains <Context path="/PPP" displayName="PPP" docBase="/home/me/documents" reloadable="true" crossContext="true">... (1 Reply)
Discussion started by: linuxadmin
1 Replies

5. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

6. Shell Programming and Scripting

variable inside variable inside loop headache

Hi Gurus I have a file called /tmp/CMDB which looks like this serial: 0623AN1208 hostname: server1 model: x4100 assetID: 1234 I am writing a for loop that will go through this file line by line creating a variable of itself. Using the first iteration of the loop (i.e. the first line) as... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

7. Shell Programming and Scripting

Need help in sed command ( Replacing a pattern inside a file with a variable value )

Hello, The following sed command is giving error sed: -e expression #1, char 13: unknown option to `s' The sed command is echo "//-----" | sed "s/\/\/---*/$parChk/g" where parChk="//---ee-" How can i print the variable value from sed command ? And is it possible to replace a... (2 Replies)
Discussion started by: frozensmilz
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. UNIX for Dummies Questions & Answers

passing a variable inside a variable to a function

I would like to know how to pass a variable inside a variable to a function. sample code below -------------- for x in 1 9 do check_null $C$x ##call function to check if the value is null if then echo "line number:$var_cnt,... (2 Replies)
Discussion started by: KingVikram
2 Replies

10. Shell Programming and Scripting

ksh: A part of variable A's name is inside of variable B, how to update A?

This is what I tried: vara=${varb}_count (( vara += 1 )) Thanks for help (4 Replies)
Discussion started by: pa3be
4 Replies
Login or Register to Ask a Question