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

[Q] String substitution...

Status
Not open for further replies.

CyanBlue

Technical User
Mar 6, 2002
16
US
Hi...

I am trying to do some string substitution...

I have a file called OBJList1.txt and it's content goes like this...
---
Sphere1 Cone1 Cube1 Cylinder1 Sphere2 Cone2 pCube1 Cylinder2
---

In one of my functions, I need to delete a certain word from the file and update the file at the same time...
So, I tried to use sed and it didn't work for me...
---
sed 's/${toDelete}//g' $OBJLIST1 > $OBJLIST1
---

Say that the word to delete is 'Cube1' and the variable 'toDelete' gets the value, and the output should be like this, but I cannot make it working... :(
---
Sphere1 Cone1 Cylinder1 Sphere2 Cone2 pCube1 Cylinder2
---

I am trying to figure this out for an hour with several variations but no luck yet... :(

Can someone tell me how to do this???

TIA

Jason
 
You need a new file to write the amended output to
Try:-

sed 's/${toDelete}//g' $OBJLIST1 > $OBJLIST1.tmp

then you can rename the temporary file back to the
original name : -

mv $OBJLIST1.tmp $OBJLIST1



Dickie Bird
db@dickiebird.freeserve.co.uk
 
Hi, dickiebird...

Thank you for the reply...
I have tried to save it to some other file and renameing it to the original file name, and it didn't work...
What I think is that somehow sed doesn't take the variable names or I just don't know how to do it...
I tried following on the bash prompt...
---
$ sed 's/Cube1 //g' OBJList1.txt ---> this works...
---
$ a="Cylinder1 "
$ sed 's/${a}//g' OBJList1.txt ---> this doesn't work...
---
I tried using $a, ${a}, "${a}" and so on and none of those variations worked for me...

Does anybody know why and how???

TIA

Jason
 
Hi,

the shell doesn't make the substitution of the your variable because the expression is between '.

Try :

$ sed "s/${a}//g" OBJList1.txt

Jean Pierre.
 
Thank you, Jean...

That does the trick...
Appreciate your help..
:)

Jason
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top