LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-14-2012, 09:52 AM   #1
abhitanshu
LQ Newbie
 
Registered: Jul 2011
Posts: 6

Rep: Reputation: Disabled
Problem while replacing text between 2 strings


I have a file-
[SIZE="1"]<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


here i want to replace server name from gIntegraHost to gTestRunHost but only between <TCPSampler and curl but after firing the below command only first instance is getting replaced not the other one.

sed '1,/<TCPSampler/!{ /curl.*END/,/<TCPSampler/!s/{gIntegraHost}/{gTestRunHost}/; }' file1 >aaa

Please suggest if i can do anything to solve it.
 
Old 06-15-2012, 03:02 AM   #2
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 11,062

Rep: Reputation: Disabled
Please edit your post and and surround the content of the file with the CODE tags making it ending with [/CODE] and beginning with [CODE] to make it easier to read.

Last edited by Didier Spaier; 06-15-2012 at 03:04 AM.
 
Old 06-15-2012, 03:48 AM   #3
abhitanshu
LQ Newbie
 
Registered: Jul 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
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 &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.
 
Old 06-15-2012, 09:10 AM   #4
Valdis Grinbergs
Member
 
Registered: Dec 2005
Distribution: Debian
Posts: 30

Rep: Reputation: 25
The following perl script should work.
At a command prompt, type perl scriptname.pl < inputfile > outputfile

If you find a better or shorter way, please post it here for us to see!

<CODE>
use strict;
use warnings;

my $line;
my $inside_tag = 0;
my $has_curl = 0;

while ($line = <>) {
if ($line =~ /<TCPSampler/) {
$inside_tag = 1;
}
if (lc($line) =~ /<curl/) {
$has_curl = 1;
}
if ($line =~ /<\/TCPSampler/) {
$inside_tag = 0;
$has_curl = 0;
}
if ($inside_tag and $has_curl) {
$line =~ s/gIntegraHost/gTestRunHost/g;
}
print $line;
}
</CODE>
 
Old 06-15-2012, 09:26 AM   #5
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 11,062

Rep: Reputation: Disabled
... or replacing the angle brackets <> surrounding CODE by square ones [] for better readability :
Code:
use strict;
use warnings;

my $line;
my $inside_tag = 0;
my $has_curl = 0;

while ($line = <>) {
    if ($line =~ /<TCPSampler/) {
        $inside_tag = 1;
    }
    if (lc($line) =~ /<curl/) {
        $has_curl = 1;
    }
    if ($line =~ /<\/TCPSampler/) {
        $inside_tag = 0;
        $has_curl = 0;
    }
    if ($inside_tag and $has_curl) {
        $line =~ s/gIntegraHost/gTestRunHost/g;
    }
    print $line;
}

Last edited by Didier Spaier; 06-15-2012 at 09:28 AM.
 
Old 06-16-2012, 03:04 AM   #6
abhitanshu
LQ Newbie
 
Registered: Jul 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Sorry. It did not work. gIntegraHost did not get replaced at any place.
 
Old 06-16-2012, 05:31 AM   #7
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Try this:
Code:
sed  '/<TCPSampler/{:a;N;/<\/TCPSampler/!ba; /curl/s/{gIntegraHost}/{gTestRunHost}/g}' input_file
The logic is as follows: collect all lines between '<TCPSampler' and '</TCPSampler' in the pattern space; after that if there is 'curl' in pattern space, then perform substitution globally (note the `g' flag).
 
Old 06-18-2012, 08:49 AM   #8
abhitanshu
LQ Newbie
 
Registered: Jul 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Sorry it did not work.
Is it working for you..?
 
Old 06-18-2012, 09:13 AM   #9
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Quote:
Originally Posted by abhitanshu View Post
Sorry it did not work.
Is it working for you..?
Yes, it works for me. Please copy-paste corresponding text from terminal here, I'm just curious what's wrong. Also post input data using the [CODE]..[/CODE] tags to preserve formatting.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Searching and replacing strings in a file with strings in other files xndd Linux - Newbie 16 07-29-2010 02:40 PM
Python: replacing strings in a file tonya11en Programming 2 05-19-2009 06:52 PM
replacing strings in many files using tcsh mcbenus Linux - Software 5 03-03-2008 05:50 PM
Help with sed - replacing strings thulley Linux - Software 4 08-22-2006 10:07 AM
Using c++ to write to a text file. Im having problem with strings swatward Programming 4 02-03-2005 09:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration