![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 05: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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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.... |
|
#3
|
|||
|
|||
|
Thanks for excelent answers !! It works....
__________________
Regards Trond |
|||
| Google The UNIX and Linux Forums |