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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

replace strings with sed

Status
Not open for further replies.

minus0

Programmer
Joined
Feb 20, 2003
Messages
73
Location
US
Hello All,

I am trying to replace a string in multiple files using sed but its not fully doing what I am trying to.
say I have myvariable=blah blah in multiple files, the value of myvariable is different in different files. I want to replace whatever the value of myvariable to TEMP1.
I tried
sed 's/myvariable=/myvariable=TEMP1/g' file1 > file1.new

but the result in file1.new was
myvariable=TEMP1blah blah

I need just myvariable=TEMP1

any help is appreciated.
 
Do this:

sed "s/myVariable=oldstuff/myVariable=newstuff/g" file1 > file1.new

You weren't replacing the old value for the variable.
 
Do this:

> sed "s/myVariable=oldstuff/myVariable=newstuff/g" file1 > file1.new

> You weren't replacing the old value for the variable

oldstuff could be different in each file which is why I am looking at the variable to be replaced but not the value of the variable itself.
 
You need to be able to specify what delimits blah blah. If it is the end of the line, use

sed "s/myVariable=.*/myVariable=newstuff/g" file1 > file1.new

CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
thanks for the tip CaKiwi - that helped
 
ok, now running into a different issue

how do I refer to a variable from a sed script. for example,
#!/bin/ksh

fileName=temp

sed 's/UNIQUE_NAME=.*/UNIQUE_NAME=$fileName/' $File > $File.new

# mv $File.new $File

now when I do a diff on the File.new and File
I see that UNIQUE_NAME is having a value $fileName. How do I make sed to get the value of the variable fileName?

Thanks for any help
 
Use double quotes instead of single quotes around the sed command.

CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
never mind guys - figured it out. In case some one else runs into a similar issue I am including what I did to get it to work

old way
sed 's/UNIQUE_NAME=.*/UNIQUE_NAME=$fileName/' $File > $File.new

new
sed "s/UNIQUE_NAME=.*/UNIQUE_NAME=$fileName/" $File > $File.new
 
Try this:
Code:
sed 's/UNIQUE_NAME=.*/UNIQUE_NAME='"$fileName"'/' $File > $File.new && mv $File.new $File
or this:
Code:
echo '1,$s/UNIQUE_NAME=.*/UNIQUE_NAME='"$fileName"'/\nwq' | ex $File



Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top