Results 1 to 6 of 6
Hey,
I would like to add a tag <here> </here> below the second occurrence of <bb> tag.
Code:
<aa> </aa>
<bb> </bb> -- 1st occurrence
<cc> </cc>
<bb> </bb> -- ...
- 08-18-2010 #1
sed question
Hey,
I would like to add a tag <here> </here> below the second occurrence of <bb> tag.
I tried this one, but in vain. Any idea?Code:<aa> </aa> <bb> </bb> -- 1st occurrence <cc> </cc> <bb> </bb> -- 2nd occurrence <here> </here> <dd> </dd>
Code:sed -en '/<bb>/2' -e '/<bb>/a<here> </here>/' test
- 08-19-2010 #2Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
If awk is allowed:
Code:awk '/<bb>/ && ++c==2 {$0=$0 RS "<here> </here>"}1' file
- 08-19-2010 #3
Thanks for your update. If possible, please explain the awk command that you have used ...
- 08-20-2010 #4Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Ok, here we go.
Explanation:Code:awk '/<bb>/ && ++c==2 {$0=$0 RS "<here> </here>"}1'
If a line contains <bb> increase the counter c. On the second match if the counter has the value of 2:Code:/<bb>/ && ++c==2
add the new line behind the 2nd line with <bb>Code:{$0=$0 RS "<here> </here>"}
Print the current line.Code:1
- 08-21-2010 #5
- 08-23-2010 #6


Reply With Quote
