Sed:- Supported variable replacement after string match?

Hi All,

I am trying to replace the variable in the file after the particular match string. It is being replaced if i hardcode the value and with use of "&" with sed.

 
sed -e "s/URL./& http:\\localhost:7223/g"

But when am trying to pass the variable it is failing. I tried multiple options mentioned below.

 
sed -e "s/URL./& $url/g" 
or 
sed -e "s/URL./&\ $url/g" 
or
sed -e "s/URL./&\ |$url/g" 
 

Error

 
sed: -e expression #1, char 16: unknown option to `s'

Can I anyone suggest if sed replacement support variable with "&".

put single quote or double quote for your vaiable.

 
sed -e 's/URL./&'$url'/g' 
 

or

 
sed -e "s/URL./&"$url"/g" 

Hi Milan,

It is giving the same issue.

If i go with

 
sed -e 's/URL./&"$url"/g'

it is replacing the $url as it is instead of replacing the value of variable.

For rest of the option. I am still getting the same error.

sed: -e expression #1, char 25: unknown option to `s'

you have to use either single quote or double quote everywhere..check my code,

I have tried both single quotes everywhere and double quotes everywhere.
It straight away giving me the expression error

 
sed: -e expression #1, char 25: unknown option to `s'

It works without any issue. Can you print the $url before the sed statement to see what goes into sed ?

$ url='http:\\\\localhost:7223'
$ echo "URL." | sed -e "s/URL./& $url/g"
URL. http:\\localhost:7223

Hi Raja,

I have tried that multiple time but getting the same issue.

Pasting my script.

 
#! /bin/bash
HSFILE=/home/user/Temp/Test/Log/Server2/Components.txt
num=1
while IFS="|" read url port 
do
 echo "URL is $url Port is $port"                                
 < Monitor_Template.txt sed -e "s/HawkController./& $url/g" > "/home/user/Temp/Test/Log/Server2/Server1/Monitor_Template_$num.txt"
 
 num=$(($num+1))
 echo "Welcome"
 done < $HSFILE

Input File:- Components.txt

 
tcp://localhost.com:7222|7222
tcp://localhost.com:7223|7223
tcp://localhost.com:7224|7224

I think you need to make your input file like this to work

tcp:\\/\\/localhost.com:7223|7223
tcp:\\/\\/localhost.com:7224|7224
1 Like

Take another delimiter that does not occur in $url

sed "s#URL.#& $url#"
1 Like

Thanks Made in Germany & Raja

 
sed "s#URL.#& $url#"

It is working fine for me.