Replacing part of a pattern in sed

Hi
I have a piece of xml that has a pattern like this
<int>159</int><int>30</int>
I want to find this pattern but only substitute the second part of the pattern to {rid1}.
Is that possible in sed ?
Thanks.

---------- Post updated at 12:10 PM ---------- Previous update was at 12:01 PM ----------

Actually the source xml fragment is :
<int>159</int>\n <int>30</int>
After replacement the output should be
<int>159</int>\n <int>{rid1}</int>

Thanks.

sed 's/<int>\([0-9]*\)/<int>{rid1}/2' inputFile

OR

sed 's/[0-9][0-9]*/{rid1}/2' inputFile
1 Like

Hi,
Here is my input file (r.txt):
<int>163</int>\n <int>30</int>
And my sed command in r.sed
s/<int>\([0-9]+\)<\/int>\n\( *\)<int>\([0-9]+\)<\/int>/<int>\{requestId\}<\/int>/2

sed -f r.sed r.txt

produced

<int>163</int>\n <int>30</int>

What am I missing ?

Thanks!

You are trying to use extended regular exp and I believe Extended Reg Exp are used by egrep (or grep -E) and awk. This is not for sed. Others~, pls correct if wrong.
Try command in post #2.

in your matching pattern, replace every following pattern

[0-9]+

by this one:

[0-9][0-9]*

I tried the following in r.sed

s/<int>\([0-9][0-9]\)<\/int>\n\( *\)<int>\([0-9][0-9]\)<\/int>/<int>\{requestId\}<\/int>/2

No luck. Same output as before:
<int>163</int>\n <int>30</int>

If you'd post your full input file as well as a reprensentative example of the expected output , it would help.

Here is just a little example , i hope it helps.

# echo '<int>163</int>\n <int>30</int>\n<int>163</int>\n <int>30</int>'
<int>163</int>
 <int>30</int>
<int>163</int>
 <int>30</int>
# echo '<int>163</int>\n <int>30</int>\n<int>163</int>\n <int>30</int>' | sed '/163/N;s:>[0-9][0-9]*<:>{rid1}<:2'
<int>163</int>
 <int>{rid1}</int>
<int>163</int>
 <int>{rid1}</int>
#

The input file i am testing with has just one line in it:

<int>163</int>\n  <int>30</int>

This xml fragment is actually part of a web request recorded by a load testing tool. Since there are several places within the web request that numbers can occur, it is important to identify the surrounding tags <int> and </int> when matching pattern.
I did not understand your command but modified it slightly to remove the reference to 163:

sed '/[0-9][0-9]*/N;s:>[0-9][0-9]*<:>{requestId}<:2'

This returned an error:

c:>sed -f r.sed r.txt
sed: file r2.sed line 1: unknown option to 's'

I am testing this on windows with sedforwindows utility.

What does "N;s:>" , "<:>" parts and "<:2" of your command mean ?
Thanks.

---------- Post updated at 05:23 PM ---------- Previous update was at 04:28 PM ----------

A correction:
The input file (r.txt) i am testing with has just one line in it:

<int>163</int>\n <int>30</int>

This xml fragment is actually part of a web request recorded by a load testing tool. Since there are several places within the web request that numbers can occur, it is important to identify the surrounding tags <int> and </int> when matching pattern.
The numbers can be different each time the request is recorded.

I did not understand your command completely but modified it slightly to remove the reference to 163:

sed '/[0-9][0-9]*/N;s:>[0-9][0-9]*<:>{requestId}<:2'

This returned an error:

c:>sed -f r.sed r.txt
sed: file r.sed line 1: unknown option to 's'

I am testing this on windows with sedforwindows utility.

What does "N;s:>" , "<:>" parts and "<:2" of your command mean ?
Thanks.

---------- Post updated 01-08-11 at 12:35 PM ---------- Previous update was 01-07-11 at 05:23 PM ----------

Here is a rough sample of an input web request :

web_request(abc, 
  "Method=null", 
  "TargetObjectId=/1", 
  BEGIN_ARGUMENTS, 
  "<timestamp>0</timestamp>\n  <timeToLive>0</timeToLive>\n  <headers>\n "
  "   <entry>\n      <string>DDD</string>\n      <string>"
  "</string>\n    </entry>\n    <entry>\n      <string>asnumber</string>\n   "
  "   <string>345</string>\n    </entry"
  ">\n    <entry>\n      <string>bsnumber</string>\n      <string>378"
  "</string>\n    </entry>\n  </headers>\n  <operation>handleCall</"
  "operation>\n  <parameters>\n    <int>163</int>\n    <int>30</int>\n  "
  "  <string>CANCEL</string>\n    <null/>\n    <boolean>false</boolean>"
  "\n  </parameters>\n" 
  END_ARGUMENTS, 
  LAST);

I am interested in replacing the pattern <int>163</int>\n <int>30</int> within this web request.
Note the numbers between the tags <int> and </int> can be anything. Also there are several places where numbers occur in this web request.

I tried the following sed command and it worked only if I removed the newline and the multiple spaces (there can be any number of them) between the two repeating patterns <int>number</int>.

/web_request\(abc\)/,/^LAST/{
s/<int>\([0-9][0-9]*\)<\/int><int>\([0-9][0-9]*\)<\/int>/<int>\1<\/int><int>\{rid\}<\/int>/
}

For this sed command to work I had to alter the input web request to:

web_request(abc, 
  "Method=null", 
  "TargetObjectId=/1", 
  BEGIN_ARGUMENTS, 
  "<timestamp>0</timestamp>\n  <timeToLive>0</timeToLive>\n  <headers>\n "
  "   <entry>\n      <string>DDD</string>\n      <string>"
  "</string>\n    </entry>\n    <entry>\n      <string>asnumber</string>\n   "
  "   <string>345</string>\n    </entry"
  ">\n    <entry>\n      <string>bsnumber</string>\n      <string>378"
  "</string>\n    </entry>\n  </headers>\n  <operation>handleCall</"
  "operation>\n  <parameters>\n    <int>163</int><int>30</int>\n  "
  "  <string>CANCEL</string>\n    <null/>\n    <boolean>false</boolean>"
  "\n  </parameters>\n" 
  END_ARGUMENTS, 
  LAST);
}

I want to retain the newlines and the spaces(however many of them) occurring between the two repeating elements. How can I alter my sed command to achieve this ?

Desired output:

web_request("abc", 
  "Method=null", 
  "TargetObjectId=/1", 
  BEGIN_ARGUMENTS, 
  "<timestamp>0</timestamp>\n  <timeToLive>0</timeToLive>\n  <headers>\n "
  "   <entry>\n      <string>DDD</string>\n      <string>"
  "</string>\n    </entry>\n    <entry>\n      <string>asnumber</string>\n   "
  "   <string>345</string>\n    </entry"
  ">\n    <entry>\n      <string>bsnumber</string>\n      <string>378"
  "</string>\n    </entry>\n  </headers>\n  <operation>handleCall</"
  "operation>\n  <parameters>\n    <int>163</int>\n    <int>{rid}</int>\n  "
  "  <string>CANCEL</string>\n    <null/>\n    <boolean>false</boolean>"
  "\n  </parameters>\n" 
  END_ARGUMENTS, 
  LAST);

Thanks.

Any suggestions on this ?
Thanks.

For me it works :

# cat ts1
web_request(abc,
"Method=null",
"TargetObjectId=/1",
BEGIN_ARGUMENTS,
"<timestamp>0</timestamp>\n <timeToLive>0</timeToLive>\n <headers>\n "
" <entry>\n <string>DDD</string>\n <string>"
"</string>\n </entry>\n <entry>\n <string>asnumber</string>\n "
" <string>345</string>\n </entry"
">\n <entry>\n <string>bsnumber</string>\n <string>378"
"</string>\n </entry>\n </headers>\n <operation>handleCall</"
"operation>\n <parameters>\n <int>163</int><int>30</int>\n "
" <string>CANCEL</string>\n <null/>\n <boolean>false</boolean>"
"\n </parameters>\n"
END_ARGUMENTS,
LAST);
}
# sed '/[0-9][0-9]*<\/int>/N;s:<int>[0-9][0-9]*<:<int>{rid1}<:2' ts1
web_request(abc,
"Method=null",
"TargetObjectId=/1",
BEGIN_ARGUMENTS,
"<timestamp>0</timestamp>\n <timeToLive>0</timeToLive>\n <headers>\n "
" <entry>\n <string>DDD</string>\n <string>"
"</string>\n </entry>\n <entry>\n <string>asnumber</string>\n "
" <string>345</string>\n </entry"
">\n <entry>\n <string>bsnumber</string>\n <string>378"
"</string>\n </entry>\n </headers>\n <operation>handleCall</"
"operation>\n <parameters>\n <int>163</int><int>{rid1}</int>\n "
" <string>CANCEL</string>\n <null/>\n <boolean>false</boolean>"
"\n </parameters>\n"
END_ARGUMENTS,
LAST);
}

or just

# sed 's:<int>[0-9][0-9]*<:<int>{rid1}<:2' ts1
web_request(abc,
"Method=null",
"TargetObjectId=/1",
BEGIN_ARGUMENTS,
"<timestamp>0</timestamp>\n <timeToLive>0</timeToLive>\n <headers>\n "
" <entry>\n <string>DDD</string>\n <string>"
"</string>\n </entry>\n <entry>\n <string>asnumber</string>\n "
" <string>345</string>\n </entry"
">\n <entry>\n <string>bsnumber</string>\n <string>378"
"</string>\n </entry>\n </headers>\n <operation>handleCall</"
"operation>\n <parameters>\n <int>163</int><int>{rid1}</int>\n "
" <string>CANCEL</string>\n <null/>\n <boolean>false</boolean>"
"\n </parameters>\n"
END_ARGUMENTS,
LAST);
}

There is no newline between the patterns<int>163</int> and <int>30</int>\n in your input request :

# cat ts1
web_request(abc,
"Method=null",
"TargetObjectId=/1",
BEGIN_ARGUMENTS,
"<timestamp>0</timestamp>\n <timeToLive>0</timeToLive>\n <headers>\n "
" <entry>\n <string>DDD</string>\n <string>"
"</string>\n </entry>\n <entry>\n <string>asnumber</string>\n "
" <string>345</string>\n </entry"
">\n <entry>\n <string>bsnumber</string>\n <string>378"
"</string>\n </entry>\n </headers>\n <operation>handleCall</"
"operation>\n <parameters>\n <int>163</int><int>30</int>\n "
" <string>CANCEL</string>\n <null/>\n <boolean>false</boolean>"
"\n </parameters>\n"
END_ARGUMENTS,
LAST);
}

What the input request should be:

web_request(abc, 
"Method=null", 
"TargetObjectId=/1", 
BEGIN_ARGUMENTS, 
"<timestamp>0</timestamp>\n <timeToLive>0</timeToLive>\n <headers>\n "
" <entry>\n <string>DDD</string>\n <string>"
"</string>\n </entry>\n <entry>\n <string>asnumber</string>\n "
" <string>345</string>\n </entry"
">\n <entry>\n <string>bsnumber</string>\n <string>378"
"</string>\n </entry>\n </headers>\n <operation>handleCall</"
"operation>\n <parameters>\n <int>163</int>\n <int>30</int>\n "
" <string>CANCEL</string>\n <null/>\n <boolean>false</boolean>"
"\n </parameters>\n" 
END_ARGUMENTS, 
LAST);

It is when I include that newline and a few spaces between <int>163</int> and <int>30</int>\n that the sed code does not work...

Thanks.

Just use the first suggestion of my previous post :

# cat ts1
web_request(abc,
"Method=null",
"TargetObjectId=/1",
BEGIN_ARGUMENTS,
"<timestamp>0</timestamp>\n <timeToLive>0</timeToLive>\n <headers>\n "
" <entry>\n <string>DDD</string>\n <string>"
"</string>\n </entry>\n <entry>\n <string>asnumber</string>\n "
" <string>345</string>\n </entry"
">\n <entry>\n <string>bsnumber</string>\n <string>378"
"</string>\n </entry>\n </headers>\n <operation>handleCall</"
"operation>\n <parameters>\n <int>163</int>\n <int>30</int>\n "
" <string>CANCEL</string>\n <null/>\n <boolean>false</boolean>"
"\n </parameters>\n"
END_ARGUMENTS,
LAST);
}
# sed '/[0-9][0-9]*<\/int>/N;s:<int>[0-9][0-9]*<:<int>{rid1}<:2' ts1
web_request(abc,
"Method=null",
"TargetObjectId=/1",
BEGIN_ARGUMENTS,
"<timestamp>0</timestamp>\n <timeToLive>0</timeToLive>\n <headers>\n "
" <entry>\n <string>DDD</string>\n <string>"
"</string>\n </entry>\n <entry>\n <string>asnumber</string>\n "
" <string>345</string>\n </entry"
">\n <entry>\n <string>bsnumber</string>\n <string>378"
"</string>\n </entry>\n </headers>\n <operation>handleCall</"
"operation>\n <parameters>\n <int>163</int>\n <int>{rid1}</int>\n "
" <string>CANCEL</string>\n <null/>\n <boolean>false</boolean>"
"\n </parameters>\n"
END_ARGUMENTS,
LAST);
}