Problem while replacing text between two tags

Hi,
I have a file i which i want to replace {gIntegraHost} to {gTestRunHost} between <TCPSampler and curl only.
I am firing below command

 sed '1,/<TCPSampler/!{ /curl/,/<TCPSampler/!s/{gIntegraHost}/{gTestRunHost}/; }' file1

But only on instance is getting replaced not all.

Any idea how to do it..?

Thanks in advance.

Please do not leave people guessing. Show a representative sample of input, desired output, attempts at a solution and specify what OS and versions being used, or this thread will be closed.

I am using Red hat Linux 6.2.

Sample Input is -

<TCPSampler guiclass="TCPSamplerGui" testclass="TCPSampler" testname="Req Request" enabled="true">
            <stringProp name="TCPSampler.server">${gIntegraHost}</stringProp>
            <stringProp name="TCPSampler.port">${gTcpShellPort}</stringProp>
END
</stringProp>
          </TCPSampler>
            <stringProp name="TCPSampler.server">${gIntegraHost}</stringProp>
            <boolProp name="TCPSampler.reUseConnection">false</boolProp>
rm -f resp.data
curl -v -x ${gIntegraHost}:${gIntegraPort} http://${gTestOSHost}${gTestOSPath}video01.mp4 -H "Pragma: no-cache" --range 500000-1000000 -A "${lUserAgent}" -o resp.data
END
<TCPSampler guiclass="TCPSamplerGui" testclass="TCPSampler" testname="Req Request" enabled="true">
            <stringProp name="TCPSampler.server">${gIntegraHost}</stringProp>
            <boolProp name="TCPSampler.nodelay">false</boolProp>
            <stringProp name="TCPSampler.request">date
END
</stringProp>
            <stringProp name="ConfigTestElement.username"></stringProp>
          </TCPSampler>
          <hashTree/>
          <TCPSampler guiclass="TCPSamplerGui" testclass="TCPSampler" testname="Curl Request 1" enabled="true">
            <stringProp name="TCPSampler.server">${gIntegraHost}</stringProp>
            <boolProp name="TCPSampler.nodelay">false</boolProp>
            <stringProp name="TCPSampler.request">cd /tmp
rm -f resp.data
curl -v -x ${gIntegraHost}:${gIntegraPort} http://${gTestOSHost}${gTestOSPath}video01.mp4 -H "Pragma: no-cache" --range 500000-1000000 -A "${lUserAgent}" -o resp.data
END<TCPSampler guiclass="TCPSamplerGui" testclass="TCPSampler" testname="Req Request" enabled="true">
            <stringProp name="TCPSampler.server">${gIntegraHost}</stringProp>
            <stringProp name="TCPSampler.port">${gTcpShellPort}</stringProp>
            <stringProp name="TCPSampler.timeout"></stringProp>
            <stringProp name="TCPSampler.request">date
END
</stringProp>
            <stringProp name="ConfigTestElement.username"></stringProp>
            <stringProp name="ConfigTestElement.password"></stringProp>
          </TCPSampler>
          <hashTree/>
<TCPSampler guiclass="TCPSamplerGui" testclass="TCPSampler" testname="Curl Request 1" enabled="true">
            <stringProp name="TCPSampler.server">${gIntegraHost}</stringProp>
            <boolProp name="TCPSampler.nodelay">false</boolProp>
            <stringProp name="TCPSampler.request">cd /tmp
rm -f resp.data
curl -v -x ${gIntegraHost}:${gIntegraPort} http://${gTestOSHost}${gTestOSPath}video01.mp4 -H "Pragma: no-cache" --range 500000-1000000 -A "${lUserAgent}" -o resp.data
END

and Sample output should be -

<TCPSampler guiclass="TCPSamplerGui" testclass="TCPSampler" testname="Req Request" enabled="true">
            <stringProp name="TCPSampler.server">${gIntegraHost}</stringProp>
            <stringProp name="TCPSampler.port">${gTcpShellPort}</stringProp>
END
</stringProp>
          </TCPSampler>
            <stringProp name="TCPSampler.server">${gTestRunHost}</stringProp>
            <boolProp name="TCPSampler.reUseConnection">false</boolProp>
rm -f resp.data
curl -v -x ${gIntegraHost}:${gIntegraPort}  http://${gTestOSHost}${gTestOSPath}video01.mp4 -H "Pragma:  no-cache" --range 500000-1000000 -A  "${lUserAgent}" -o resp.data
END
<TCPSampler guiclass="TCPSamplerGui" testclass="TCPSampler" testname="Req Request" enabled="true">
            <stringProp name="TCPSampler.server">${gIntegraHost}</stringProp>
            <boolProp name="TCPSampler.nodelay">false</boolProp>
            <stringProp name="TCPSampler.request">date
END
</stringProp>
            <stringProp name="ConfigTestElement.username"></stringProp>
          </TCPSampler>
          <hashTree/>
          <TCPSampler guiclass="TCPSamplerGui" testclass="TCPSampler" testname="Curl Request 1" enabled="true">
            <stringProp name="TCPSampler.server">${gTestRunHost}</stringProp>
            <boolProp name="TCPSampler.nodelay">false</boolProp>
            <stringProp name="TCPSampler.request">cd /tmp
rm -f resp.data
curl -v -x ${gIntegraHost}:${gIntegraPort}  http://${gTestOSHost}${gTestOSPath}video01.mp4 -H "Pragma:  no-cache" --range 500000-1000000 -A  "${lUserAgent}" -o resp.data
END<TCPSampler guiclass="TCPSamplerGui" testclass="TCPSampler" testname="Req Request" enabled="true">
            <stringProp name="TCPSampler.server">${gIntegraHost}</stringProp>
            <stringProp name="TCPSampler.port">${gTcpShellPort}</stringProp>
            <stringProp name="TCPSampler.timeout"></stringProp>
            <stringProp name="TCPSampler.request">date
END
</stringProp>
            <stringProp name="ConfigTestElement.username"></stringProp>
            <stringProp name="ConfigTestElement.password"></stringProp>
          </TCPSampler>
          <hashTree/>
<TCPSampler guiclass="TCPSamplerGui" testclass="TCPSampler" testname="Curl Request 1" enabled="true">
            <stringProp name="TCPSampler.server">${gTestRunHost}</stringProp>
            <boolProp name="TCPSampler.nodelay">false</boolProp>
            <stringProp name="TCPSampler.request">cd /tmp
rm -f resp.data
curl -v -x ${gIntegraHost}:${gIntegraPort}  http://${gTestOSHost}${gTestOSPath}video01.mp4 -H "Pragma:  no-cache" --range 500000-1000000 -A  "${lUserAgent}" -o resp.data
END

and i am using below command -

sed '1,/<TCPSampler/!{ /curl/,/<TCPSampler/!s/{gIntegraHost}/{gTestRunHost}/; }' file1

---------- Post updated at 10:47 AM ---------- Previous update was at 10:39 AM ----------

I have pasted my desired output in previous post.

I am getting gIntegraHost replaced with gTestRunHost only at the first instance with my command not the other 2.

awk '   /^TCPSampler/   { S=1 }
        /^\/TCPSampler/ { S=0 }
        S { for(N=1; N<=NF; N++) if ($N ~ /curl/) gsub(/{gIntegraHost}/, "{gTestRunHost}", $N) }
        1' RS="<" ORS="<" FS="\n" xmlfile > newfile

Use nawk on solaris.

Sorry it did not work.

I got gIntegraHost everywhere. It did not get replaced at all. I want gIntegraHost to be replaced with gTestRunHost only where there in curl between<TCPSampler and </TCPSampler>. In below case it should not get replaced

<TCPSampler guiclass="TCPSamplerGui" testclass="TCPSampler" testname="Date" enabled="true">
<stringProp name="TestPlan.comments">This will clear any existing content in the webdav cache</stringProp>
<stringProp name="TCPSampler.server">${gIntegraHost}</stringProp>
<boolProp name="TCPSampler.reUseConnection">false</boolProp>
<stringProp name="TCPSampler.port">${gTcpShellPort}</stringProp>
<boolProp name="TCPSampler.nodelay">false</boolProp>
<stringProp name="TCPSampler.timeout"></stringProp>
<stringProp name="TCPSampler.request">date
END
</stringProp>
<stringProp name="ConfigTestElement.username"></stringProp>
<stringProp name="ConfigTestElement.password"></stringProp>
</TCPSampler>

And in This case it should be replaced

<TCPSampler guiclass="TCPSamplerGui" testclass="TCPSampler" testname="Curl Request 1" enabled="true">
<stringProp name="TCPSampler.server">${gIntegraHost}</stringProp>
<boolProp name="TCPSampler.reUseConnection">false</boolProp>
<stringProp name="TCPSampler.port">${gTcpShellPort}</stringProp>
<boolProp name="TCPSampler.nodelay">false</boolProp>
<stringProp name="TCPSampler.timeout"></stringProp>
<stringProp name="TCPSampler.request">cd /tmp
rm -f resp.data
curl -v -x ${gIntegraHost}:${gIntegraPort} http://${gTestOSHost}${gTestOSPath}video01.mp4 -H "Pragma: no-cache" --range 500000-1000000 -A "${lUserAgent}" -o resp.data
END
</stringProp>
<stringProp name="ConfigTestElement.username"></stringProp>
<stringProp name="ConfigTestElement.password"></stringProp>
</TCPSampler>

Hope i explained more clearly now.

My code actually does that, though I might have edited it out from beneath you(I briefly posted a version which didn't). However it has another minor error, which I will correct now.

awk '   /<TCPSampler/   { S=1 }
        /<\/TCPSampler/ { S=0 }

        S { for(N=1; N<=NF; N++)
                if ($N ~ /curl/) gsub(/{gIntegraHost}/, "{gTestRunHost}", $N)
        } $NF
        END { printf("\n"); }' RS=">" ORS=">" FS="\n" OFS="\n" input > output

What about this one?

awk '/<\/TCPSampler/,/rm.*resp.data/{$0 ~ /gIntegraHost/;sub(/gIntegraHost/,"gTestRunHost",$0)};1' infile >outfile