Replacing part of a pattern in sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing part of a pattern in sed
# 8  
Old 01-08-2011
The input file i am testing with has just one line in it:
Code:
<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:
Code:
sed '/[0-9][0-9]*/N;s:>[0-9][0-9]*<:>{requestId}<:2'

This returned an error:
Code:
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:
Code:
<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:
Code:
sed '/[0-9][0-9]*/N;s:>[0-9][0-9]*<:>{requestId}<:2'

This returned an error:
Code:
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 :
Code:
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>.
Code:
/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:
Code:
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:
Code:
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.

Last edited by Scott; 01-11-2011 at 12:49 PM.. Reason: Please use code tags
# 9  
Old 01-10-2011
Any suggestions on this ?
Thanks.
# 10  
Old 01-11-2011
For me it works :

Code:
# 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);
}

Code:
# 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
Code:
# 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);
}


Last edited by ctsgnb; 01-11-2011 at 12:51 PM..
# 11  
Old 01-11-2011
There is no newline between the patterns<int>163</int> and <int>30</int>\n in your input request :
Code:
# 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:
Code:
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.

Moderator's Comments:
Mod Comment Please use code tags

Last edited by Scott; 01-11-2011 at 12:51 PM.. Reason: Code tags, please...
# 12  
Old 01-11-2011
Just use the first suggestion of my previous post :

Code:
# 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);
}

Code:
# 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);
}


Last edited by ctsgnb; 01-11-2011 at 01:12 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

2. Shell Programming and Scripting

Replacing part of the sentence using echo and sed

Hi, Iam using ksh and trying to execute the following syntax to replace one word of the sentence with a new word. But somehow sed is not able to replace the old value with new value. Please let me know where Iam going wrong. Sample Code : --> export line="VORTEX,abcdef" export... (3 Replies)
Discussion started by: ajithab
3 Replies

3. Shell Programming and Scripting

sed command for copying the contents of other file replacing it another file on specifc pattern

We have 2 file XML files - FILE1.XML and FILE2.xml - we need copy the contents of FILE1.XML and replace in FILE2.xml pattern "<assignedAttributeList></assignedAttributeList>" FILE1.XML 1. <itemList> 2. <item type="Manufactured"> 3. <resourceCode>431048</resourceCode> 4. ... (0 Replies)
Discussion started by: balrajg
0 Replies

4. Shell Programming and Scripting

help - sed - insert space between string of form XxxAxxBcx, without replacing the pattern

If the string is of the pattern XxxXyzAbc... The expected out put from sed has to be Xxx Xyz Abc ... eg: if the string is QcfEfQfs, then the expected output is Qcf Ef Efs. If i try to substitute the pattern with space then the sed will replace the character or pattern with space,... (1 Reply)
Discussion started by: frozensmilz
1 Replies

5. Shell Programming and Scripting

sed - replacing on the right of a pattern and looking for exact word?

how would you get SED to do the following, say you have the following lines in a text file: user=tigger some text some text some text some text some text some text user=ted some text some text some text some text some text some text user=thekingofrockandroll you want to find any line... (15 Replies)
Discussion started by: rich@ardz
15 Replies

6. Shell Programming and Scripting

Selecting a part of the text (regex pattern, awk, sed)

Hello, let's start by giving you guys a few examples of the text: "READ /TEXT123/ABC123" "READ /TEXT123/ABC123/" "READ TEXT123/ABC123" "READ TEXT123/ABC123/" "READ TEXT123/TEXT456/ABC123" "READ /TEXT123/TEXT456/ABC123" "READ /TEXT123/TEXT456/ABC123/" TEXT and ABC can be and I... (5 Replies)
Discussion started by: TehOne
5 Replies

7. Shell Programming and Scripting

Replacing or removing a long list of pattern by using awk or sed

Input: >abc|123456|def|EXIT| >abc|203456|def|EXIT2| >abc|234056|def|EXIT3| >abc|340056|def|EXIT4| >abc|456000|def|EXIT5| . . . Output: def|EXIT| def|EXIT2| def|EXIT3| def|EXIT4| def|EXIT5| . . My try code: (9 Replies)
Discussion started by: patrick87
9 Replies

8. Shell Programming and Scripting

sed remembering part of pattern using \1 concept

I am using this concept to fetch value of IP address after node= in this line of csv text: a="Measurement:,OutInviteResponseTime,Sessionid=1860700092328051458,node=67.178.40.168,nodeName=abcd,protocol=GK,25523000" echo $a | sed 's/.*node=\(.*\).*/\1/' But this outputs: ... (10 Replies)
Discussion started by: indianjassi
10 Replies

9. Shell Programming and Scripting

Need help in sed command ( Replacing a pattern inside a file with a variable value )

Hello, The following sed command is giving error sed: -e expression #1, char 13: unknown option to `s' The sed command is echo "//-----" | sed "s/\/\/---*/$parChk/g" where parChk="//---ee-" How can i print the variable value from sed command ? And is it possible to replace a... (2 Replies)
Discussion started by: frozensmilz
2 Replies

10. Shell Programming and Scripting

Sed and replacing one occurence of pattern

I would like to use sed to replace one occurence of a pattern in a file. When I use the s/// command it replaces all occurences of the pattern in the file. Should I be using something other than sed? Thanks (6 Replies)
Discussion started by: ss9u
6 Replies
Login or Register to Ask a Question