VectorLinux

The nuts and bolts => Technical General => Topic started by: Daniel on May 13, 2009, 11:59:17 am

Title: "sed" question
Post by: Daniel on May 13, 2009, 11:59:17 am
I am trying to use the sed command in a script. I need to make the command something like this:

sed -i 's,text-to-replace,variable-to-replace-it-with,g' file-to-do-this-in.txt

I have tried things like: sed -i 's,text,'$variable',g' file.txt and it says there is an incomplete 's' command. Can anybody help? Please ask for clarification if needed.
Title: Re: "sed" question
Post by: Daniel on May 13, 2009, 08:18:20 pm
Never mind, I figured it out. The solution was: sed 's/text/'$variable'/g' file.txt    if anybody is interested.
Title: Re: "sed" question
Post by: no2thesame on May 14, 2009, 03:33:24 am
Very interested. I need something just like that.
Thanks
Bruce
Title: Re: "sed" question
Post by: Daniel on June 10, 2009, 04:59:31 pm
Ok, slightly different problem with sed. I need to do this:

sed -i 's|'$(grep 'text' /file to look in)'|'$(grep 'text' /different file to look in)'|g' /file to do this in

Whenever I try to run this command, it says "unterminated `s' command".
Help anyone?
Title: Re: "sed" question
Post by: nightflier on June 10, 2009, 06:21:33 pm
I think the problem is with your single quotation marks. The interpreter reads what's between the two first ones, as in:
sed -i 's|'
Try removing all single quotation marks except the first and last.
Title: Re: "sed" question
Post by: Daniel on June 11, 2009, 06:49:19 am
If I take away the single quotation marks around the variables, it doesn't recognize them as variables anymore. Just text.
Title: Re: "sed" question
Post by: rbistolfi on June 11, 2009, 08:33:43 am
try following nightflier suggestion but using double quotes instead
Title: Re: "sed" question
Post by: Daniel on June 11, 2009, 08:48:36 am
rbistolfi: can you elaborate on that?
Title: Re: "sed" question
Post by: uelsk8s on June 11, 2009, 08:53:03 am
sed -i 's|"$(grep text /file to look in)"|"$(grep text /different file to look in)"|g' /file to do this in
Title: Re: "sed" question
Post by: Daniel on June 11, 2009, 08:57:31 am
It doesn't recognize the variables that way either.
Title: Re: "sed" question
Post by: nightflier on June 11, 2009, 11:41:48 am
Try using all double quotations.
Title: Re: "sed" question
Post by: Daniel on June 11, 2009, 01:18:03 pm
Nope, still doesn't work. It recognizes the variables but gives the "unterminated `s' command" error.