Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

How can I use a variable in sed?

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-27-2004
tromag tromag is offline
Registered User
 
Join Date: May 2004
Last Activity: 27 May 2004, 5:55 PM EDT
Location: Norway
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Sponsored Links
    #2  
Old Unix and Linux 05-27-2004
Perderabo's Unix or Linux Image
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 27 April 2015, 10:26 AM EDT
Location: Ashburn, Virginia
Posts: 9,931
Thanks: 64
Thanked 448 Times in 264 Posts
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....Linux
The Following 2 Users Say Thank You to Perderabo For This Useful Post:
gautamsomani (04-29-2012), souvikgargi (06-26-2011)
Sponsored Links
    #3  
Old Unix and Linux 05-27-2004
tromag tromag is offline
Registered User
 
Join Date: May 2004
Last Activity: 27 May 2004, 5:55 PM EDT
Location: Norway
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Computer

Thanks for excelent answers !! It works....
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Variable not found error for a variable which is returned from stored procedure swap21783 Shell Programming and Scripting 4 03-22-2011 01:58 PM
Not able to store command inside a shell variable, and run the variable gvinayagam Shell Programming and Scripting 3 11-19-2010 05:59 AM
How to define a variable with variable definition is stored in a variable? freddy1228 Shell Programming and Scripting 1 11-12-2009 03:39 AM
Insert a line including Variable & Carriage Return / sed command as Variable lowmaster Shell Programming and Scripting 2 05-19-2009 11:26 PM
Enviornment Variable in B shell (I call it nested variable) princelinux Shell Programming and Scripting 4 07-02-2008 01:35 AM



All times are GMT -4. The time now is 09:21 AM.