Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SED and variable in a script ksh 2

Status
Not open for further replies.

loloman

Technical User
Oct 30, 2002
16
0
0
FR
how to send a variable since a script shell to an instruction sed
ex :
var1=test1
var2=test2
sed '1,$s/$var1/$var2/g'
this expression don't work in my script
 
Use single quotes around the variables.

Dickie Bird (:)-)))
 
You can play with double quotes in case of embedded space:
Code:
var1="My test1"
var2="My test2"
sed '1,$s/'"$var1/$var2/g"


Hope This Help
PH.
 
Hi,

To follow onto the above question. How do I get away with using a variable in sed that contains a slash?
Eg

var1="ethernet0/0"
sed '1,$s/'"$var1/TEST/g"

This gives me an output of :
sed: command garbled: 1,$s/ethernet0/0/TEST/g

My aim is to used sed to give me just the paragraph between var1 and whatever like so:
sed -n '/$var1/,/whatever/p'

Any ideas, or perhaps a better way to perform the task?

Thanks .. TJ
 
Try something like this:
Code:
var1="ethernet0/0"
sed '1,$s!'"$var1!TEST!g"


Hope This Help
PH.
 
Thanks PHV,

That works great for replacing the variable :)

I can more or less figure out how it works but when I try using your sample to perform sed to display a portion of data then I keep getting errors.

So somewhere in my string based trying to use your working one, something is not right:
sed -n '!'"$var1!,!cdp!p"

I tried moving the ! around , the ` around the " around ;-)
I'm stummped.

Cheers ..TJ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top