How to reference a variable within sed?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to reference a variable within sed?
# 1  
Old 06-27-2007
How to reference a variable within sed?

Hi all,

How can I use sed to perform a substitution if the string that I'm going to substitute is stored in a variable:

Let's say:

sed 's/abcdefg/good'

VS

tmp="abcdefg"
sed 's/$tmp/good'

The second case doesn't work. Guess it's due to the single quotes on the outside. How can I get sed to reference the value store in variable tmp, rather than the string "$tmp"?

Thanks a bunch!
# 2  
Old 06-27-2007
Quote:
Originally Posted by rockysfr
tmp="abcdefg"
sed 's/$tmp/good'

The second case doesn't work. Guess it's due to the single quotes on the outside. How can I get sed to reference the value store in variable tmp, rather than the string "$tmp"?
Replace the single quotes with double quotes. Single quotes prevent variable expansion.

Code:
tmp="abcdefg"
sed "s/${tmp}/good"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - use back reference in 2nd command

I have data that looks like this: <Country code="US"><tag>adsf</tag><tag>bdfs</tag></Country><Country code="CA"><tag>asdf</tag><tag>bsdf</tag></Country> I want to grab the country code save it, then drop each new "<..." onto a new line with the country code added to the beginning of each So,... (9 Replies)
Discussion started by: JenniferAmon
9 Replies

2. UNIX for Dummies Questions & Answers

Extract text in sed using back reference

i have a text 20 21 22 23 24 25 26 i want to get 22 using sed back reference. I have used sed 's/{6}\(..\).*/\1/' but, it does not work. I am missing something somewhere. Please help. (5 Replies)
Discussion started by: gotamp
5 Replies

3. Shell Programming and Scripting

sed back reference error

I am trying to change a single line of a special file whose comment character is ! to show a path to the file in the comment. such as: !!HFSS and mcm path: \Signal_Integrity\Package_SI\Section_Models\C4toTrace\28nm\D6HS\SLC_5-2-5\GZ41_ICZ\NSSS\ to a different path and replace the !!HFSS... (1 Reply)
Discussion started by: mobrien601
1 Replies

4. Shell Programming and Scripting

sed error: invalid reference

Hello all, I am using sed to parse a particular part of a string and am having problems. I am getting the following error: sed: -e expression #1, char 28: invalid reference \1 on `s' command's RHS Here is the code I am using: echo "Alarm SET:" echo "" echo "Date: " $DATE echo... (4 Replies)
Discussion started by: dlundwall
4 Replies

5. Shell Programming and Scripting

Perl de-reference code reference variable

Guys, May i know how can we de reference the code reference variable.? my $a = sub{$a=shift;$b=shift;print "SUM:",($a+$b),"\n";}; print $a->(4,5); How can we print the whole function ? Please suggest me regarding this. Thanks for your time :) Cheers, Ranga :) (0 Replies)
Discussion started by: rangarasan
0 Replies

6. Shell Programming and Scripting

Perl: accessing reference to variable inside hash.

Below is hash which contains reference to variables: my %mandatoryFields = ( 1 => \$msgtype, 2 => \$switchtype, 3 => \$card_nbr, 4 => \$natv_tran_type_code, 5 => \$amt_1 ); This... (0 Replies)
Discussion started by: som.nitk
0 Replies

7. Shell Programming and Scripting

BASH - Reference external variable name dynamically

Hi there, I have included an external properties file into my BASH script via the 'source' command. I am attempting to dynamically assign a variable in the BASH script, that references the variable name within the external properties file i.e. #!/bin/bash pth=${0%/*} source... (3 Replies)
Discussion started by: mjwoodford
3 Replies

8. Shell Programming and Scripting

subsequently reference variable

Hello, This is not homework. It is a question that I received on a recent interview for a linux position. Can someone shed some light on the right answer? I got it wrong. Thanks, jaysunn (3 Replies)
Discussion started by: jaysunn
3 Replies

9. Shell Programming and Scripting

Unix Variable Reference and Substitution

I can't seem to make what appears to be a simple substitution. I want to define a list of systems for which daily reports need to be filed systems="systemA systemC systemZ" I then want to run a loop for i in ${systems} Analyze statistics Create the reports mailx (8 Replies)
Discussion started by: mugsymark
8 Replies

10. Shell Programming and Scripting

Reference Variable

Hi! I need to determin the most efficient way to do something (rather simple, I thought). I'm currently echo(ing) a series of menu options, and reading the command input as the number associated with the entry. What I need to do is when the option 1 is selected, that it references a list and... (18 Replies)
Discussion started by: cchaloux
18 Replies
Login or Register to Ask a Question