The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
passing a variable inside a variable to a function KingVikram UNIX for Dummies Questions & Answers 2 01-14-2008 04:28 PM
How to replace variable inside the variable mani_um Shell Programming and Scripting 31 08-09-2007 07:56 PM
Replace variable with a user defined variable ce124 Shell Programming and Scripting 1 04-15-2007 11:56 AM
Export command giving Variable Name vs the Value set for the Variable ParNone UNIX for Dummies Questions & Answers 2 04-03-2006 08:43 AM
ksh: A part of variable A's name is inside of variable B, how to update A? pa3be Shell Programming and Scripting 4 03-30-2005 08:29 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-27-2004
Registered User
 

Join Date: May 2004
Location: Norway
Posts: 2
How can I use a variable in sed?

Hi
I'm trying to change a part of a line with sed. Usually I will run
sed 's/mytext/mynewtext/'

Now I have a variable: var=mynewtext

sed 's/mytext/$var/' does not work. I have also tried to protect the $ with different characters but it still does'nt work.

I will be very happy if someone has a solution to this.
__________________
Regards
Trond
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-27-2004
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,355
For starters, you can switch to double quotes:

sed "s/mytext/$var/"

Sometimes that does not work. It would depend on the exact value of mytext. So another solution is to turn the single quoted string into two single quoted strings:

sed 's/mytext/'$var'/'

In this case, the 2nd single quoted string is '/'. Just using a backslash would work to that and it saves a character:

sed 's/mytext/'$var\/

There is actually no reason to quote a slash so this should also work with most shells:

sed 's/mytext/'$var/

And there may be no reason to quote anything at all, but again, the actual vakue of mytext determines this. So this may work:

sed s/mytext/$var/

Opps...you only wanted a solution, so I'll quit now....
Reply With Quote
  #3 (permalink)  
Old 05-27-2004
Registered User
 

Join Date: May 2004
Location: Norway
Posts: 2
Talking

Thanks for excelent answers !! It works....
__________________
Regards
Trond
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:17 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0