how to use a variable in sed command

Go down

how to use a variable in sed command

Post by rujuta on Mon Mar 15, 2010 7:02 pm

if a word and a file name are taken from user..now the word entered is to be replaced in the file specified by the user with another word..how to do that?
for e.g
if word entered is stored in $wrd and
filename is in $file
i tried to replace the word stored in $wrd by "hello" using the command:
sed 's/$wrd/hello/g' 0<$file
but it doesn't make changes

please suggest the answer...

rujuta

Posts : 2
Points : 4
Join date : 2010-03-09

View user profile

Back to top Go down

Re: how to use a variable in sed command

Post by Christopher on Mon Mar 15, 2010 7:21 pm

use double quotes to enclose search pattern.

Code:
$ sed "s/$word/ORANGE/g" 1.txt
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: how to use a variable in sed command

Post by rujuta on Mon Mar 15, 2010 7:27 pm

hey thank you for the solution... Very Happy

rujuta

Posts : 2
Points : 4
Join date : 2010-03-09

View user profile

Back to top Go down

Re: how to use a variable in sed command

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum