Problem while replacing text between two tags


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem while replacing text between two tags
# 1  
Old 06-14-2012
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
Code:
 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.

Last edited by Scott; 06-14-2012 at 12:06 PM.. Reason: Code tags
# 2  
Old 06-14-2012
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.
# 3  
Old 06-14-2012
I am using Red hat Linux 6.2.

Sample Input is -
Code:
<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 &quot;Pragma: no-cache&quot; --range 500000-1000000 -A &quot;${lUserAgent}&quot; -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 &quot;Pragma: no-cache&quot; --range 500000-1000000 -A &quot;${lUserAgent}&quot; -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 &quot;Pragma: no-cache&quot; --range 500000-1000000 -A &quot;${lUserAgent}&quot; -o resp.data
END


and Sample output should be -

Code:
<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 &quot;Pragma:  no-cache&quot; --range 500000-1000000 -A  &quot;${lUserAgent}&quot; -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 &quot;Pragma:  no-cache&quot; --range 500000-1000000 -A  &quot;${lUserAgent}&quot; -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 &quot;Pragma:  no-cache&quot; --range 500000-1000000 -A  &quot;${lUserAgent}&quot; -o resp.data
END

and i am using below command -

Code:
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.

Last edited by Corona688; 06-14-2012 at 12:44 PM..
# 4  
Old 06-14-2012
Code:
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.

Last edited by Corona688; 06-14-2012 at 01:01 PM..
# 5  
Old 06-14-2012
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

Code:
<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

Code:
<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 &quot;Pragma: no-cache&quot; --range 500000-1000000 -A &quot;${lUserAgent}&quot; -o resp.data
END
</stringProp>
<stringProp name="ConfigTestElement.username"></stringProp>
<stringProp name="ConfigTestElement.password"></stringProp>
</TCPSampler>

Hope i explained more clearly now.

Last edited by Scrutinizer; 06-15-2012 at 01:22 PM.. Reason: code tags
# 6  
Old 06-15-2012
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.

Code:
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

# 7  
Old 06-16-2012
What about this one?

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing text on every third line

I have file like this "copy table_name from filea.txt on node replace delimiter '|';" "copy table_name from fileb.txt on node replace delimiter '|';" "copy table_name from filec.txt on node replace delimiter'|';" "copy table_name from filee.txt on node replace delimiter '|';" "copy... (1 Reply)
Discussion started by: nnani
1 Replies

2. Shell Programming and Scripting

Replacing text/characters in vi

ok, so i have the following text to replace but it's not working. can someone please help me out: :%s~awk '// {split($2,s,",");a=$1 FS s} /-/ {b=a} END{print b}'~tail -1~g I want to replace the entire awk command with tail -1. thanks (7 Replies)
Discussion started by: SkySmart
7 Replies

3. Shell Programming and Scripting

Replacing text between Tags, with output from a complex command

The Problem I have n files all named "conf.cfg", each of which contain a line that reads: "MyVar=XXX", where XXX can be any value. I have a second file called report.xml, that might read: <Reports> <Report>Report1</Report> <Report>Report2</Report> <Report>Report3</Report> </Reports> I... (1 Reply)
Discussion started by: Flang
1 Replies

4. Shell Programming and Scripting

In PHP replacing text between TAGS & URI information in Title Tag

Hi, what I am trying to do in PHP, is to replace the Title. I need some of the URL information inside aswell depending on the domain. The title is always different so I need to store it in a variable, put the url info like described below in front of it. Here is an example how it should... (0 Replies)
Discussion started by: lowmaster
0 Replies

5. Shell Programming and Scripting

Matching and replacing text

Im new to using perl scripting, and i was wondering if anyone could help me, I need to create a cgi file that when it runs it opens a file looks through all the words, and replace the word hello with goodbye, ive been looking around and someone said to use an If statement, but ive found no other... (2 Replies)
Discussion started by: xzen123
2 Replies

6. Shell Programming and Scripting

replacing text in a file, but...

Hi all, Very first post on this forums, hope you can help me with this scripting task. I have a big text file with over 3000 lines, some of those lines contain some text that I need to replace, lets say for simplicity the text to be replaced in those lines is "aaa" and I need it to replace it... (2 Replies)
Discussion started by: Angelseph
2 Replies

7. Shell Programming and Scripting

Replacing Text in Text file

Hi Guys, I am needing some help writing a shell script to replace the following in a text file /opt/was/apps/was61 with some other path eg /usr/blan/blah/blah. I know that i can do it using sed or perl but just having difficulty writing the escape characters for it All Help... (3 Replies)
Discussion started by: cgilchrist
3 Replies

8. Shell Programming and Scripting

help required for replacing text in vi

Hi Any one pls give me command in vi to replace /opt/usr with /home/reddy/dump .... i tried escaping / with \ bt its not working... thanks in advance (6 Replies)
Discussion started by: Chandu2u
6 Replies

9. Shell Programming and Scripting

Replacing text

I was using the following code to replace the path names and it works when it is echo "$PWD/$f" | sed -e 's/^.*chris\.domain\.com/chris.domain.com/' IN fact it works great However I tried to incorporate a variable echo "$PWD/$f" | sed -e... (3 Replies)
Discussion started by: chrchcol
3 Replies

10. Shell Programming and Scripting

replacing text

hey how can i change part of a file i hve to do in a masses so mv or cp is not practical. I have to change xxx_rrr to xxx_yyy pls help thank (2 Replies)
Discussion started by: ajaya
2 Replies
Login or Register to Ask a Question