syntax for variables in sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting syntax for variables in sed
# 1  
Old 04-18-2008
syntax for variables in sed

I always kind of wondered this but I have a variable that I want to use in a search and replace. Basically I want to search a file for the string in my variable and replace it with something fixed but I'm unsure of the variable rule in sed. Here's generally what I have:

Code:
sed 's/$name/newname/g' file

Not sure if $name needs to be escaped or something. This always confuses me so if someone could help that'd be awesome! Many thanks!
# 2  
Old 04-18-2008
Quote:
Originally Posted by eltinator
I always kind of wondered this but I have a variable that I want to use in a search and replace. Basically I want to search a file for the string in my variable and replace it with something fixed but I'm unsure of the variable rule in sed. Here's generally what I have:

Code:
sed 's/$name/newname/g' file

Not sure if $name needs to be escaped or something. This always confuses me so if someone could help that'd be awesome! Many thanks!
Just remove the single quotes so that the interpolation will happen.

sed s/$name/newname/g file
# 3  
Old 04-18-2008
ah ok cool thanks! But one more problem and i believe it has to do with something im substituting in. The variable $name contains something like... "<stuff>morestuff" without the quotes of course. I think the sed command is getting killed by the <> brackets though. Any fix for this? thanks!
# 4  
Old 04-18-2008
Quote:
Originally Posted by eltinator
ah ok cool thanks! But one more problem and i believe it has to do with something im substituting in. The variable $name contains something like... "<stuff>morestuff" without the quotes of course. I think the sed command is getting killed by the <> brackets though. Any fix for this? thanks!
You can make use of backslashes(\) to negate the meaning.
If you still have doubts,revert back with proper requirement ....
# 5  
Old 04-18-2008
so i've tried the following:

Code:
sed 's/\$name/newname/g' file

and

Code:
sed 's/\$name\/newname/g' file

which doesn't error out but doesn't do anything either. I think the problem is that the variable I'm subbing in @name is of the following format:

<stuff> more stuff

So the second > isn't being escaped. Not sure if this needs to be parsed or what I can do to fix that
# 6  
Old 04-18-2008
Code:
sed "s/${name}/newname/g" file

# 7  
Old 04-18-2008
Quote:
Originally Posted by vgersh99
Code:
sed "s/${name}/newname/g" file

Hmm, seems to be giving me this problem now:

bad flag in substitute command: 'r'

I tried with and without the double quotes. I tried with single quotes around the sed and that didnt seem to do anything. Not sure if it mattered but the line before this sed command I also set the name variable to something so this isn't a variable being passed in
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Another sed Syntax Puzzle . . .

Greetings! Have a quick question for the community today; this time looking at a nifty little sed puzzle ;) Consider the following file content to be worked through:What needs to happen is theblock should be removed up to and including the following blank line, leavingI have bits and pieces... (8 Replies)
Discussion started by: LinQ
8 Replies

2. Shell Programming and Scripting

sed syntax error

Hi, In the following excerpt of shell script code: I could not understand the sed syntax. Could anyone shed some light on this? configure_ssl() { jboss_conf_file=$1 echo "Configuring SSL for -" ${jboss_conf_file} isSSLSetup=`echo cat ${jboss_conf_file} | grep <Connector... (2 Replies)
Discussion started by: royalibrahim
2 Replies

3. Shell Programming and Scripting

Problems with variables syntax

Hi there I am really struggling :eek: to place a value in a variable with the following loop, having run out of ideas please can someone point me in the right direction? We first read two PIDs of a program (say calc) into an array, then we loop reading the details of those processes into a... (6 Replies)
Discussion started by: nathan.harris
6 Replies

4. Shell Programming and Scripting

Syntax to see if two variables match

I have a simple shell script to read create a mysql database as a particular user. Right now, I have the password set as a variable to make sure someone does not mistype the password. I want to take the hard coded password out of the file and make the user input the password twice to make they... (2 Replies)
Discussion started by: bouncer
2 Replies

5. Shell Programming and Scripting

sed s/// syntax help

<tr><td width=10% style='width:5%;background:#F7F0D9;padding:0in 0in 0in 0in 0in'><center><b>Package</b></td><td width=10% valign=center style='width:5%;background:#F7F0D9;padding:0in 0in 0in 0in 0in'><center><b>JTs</b></td> This is got to be simple. I run this on the above .html file: sed... (8 Replies)
Discussion started by: dba_frog
8 Replies

6. UNIX for Dummies Questions & Answers

sed - need help for syntax

Hello, here is what I've got : FILE='/OPERATIONNEL/SATURNE/CHAMPS/MASTER/ANA/SATURNE_1DAV_20080805_20080806_S3D_T_R20080806.NC ';;... (4 Replies)
Discussion started by: Aswex
4 Replies

7. Shell Programming and Scripting

Explain following sed syntax please

Thanks to this forum I have managed to work out a solution to my problem and actually understand most of it, but one thing is confusing me and I am sure someone here can explain. I need to insert a piece of txt into a file. This txt is awk '{ sub(/$/,"\r"); print }' $JCL_WBB50103_EFTOUT >... (2 Replies)
Discussion started by: hukcjv
2 Replies

8. Shell Programming and Scripting

What syntax to use with sed c\

I know that I want to entirely replace line 3 in my file filename.txt. I have tried all sorts of variations of sed 3,3,c\replacement stuff\ filename.txt with no success. about the only thing that causes any reaction is sed 3,3c\\ filename.txt but it just prints out the whole file. ... (13 Replies)
Discussion started by: SusanDAC
13 Replies

9. Shell Programming and Scripting

Help for Sed Syntax problem

I have one File named "txt_file" # cat txt_file <DBType>RT</DBType> <AppType>RT</AppType> -------------------------------------------------- I want replace "<AppType>RT</AppType>" to <AppType>XY</AppType> in txt_file and output redirect to Newfile ... (2 Replies)
Discussion started by: SanjayLinux
2 Replies

10. UNIX for Dummies Questions & Answers

sed syntax

Hi, How can i use sed command to modify a part of a variable containing "/" by another containing "/" like describe below: VAR="/app/share/eai" VAR1="/app/share" VAR2="/data/test" echo $VAR | sed 's/... ??? # using sed to replace $VAR1 in $VAR by $VAR2 ? (4 Replies)
Discussion started by: jo_aze
4 Replies
Login or Register to Ask a Question