My server xml file has huge data part of which i'm sharing below.
I wish to add the below text held by variable "addthisline" after the closing braces i.e -->
once the first </Connector> tag is found.
addthisline="I need to be inserted after the comments"
Thus my searchstring is "</Connector>" and i need keep looking for the next "-->" entry post which I need to add the line contained in $addthisline
This injection should happen only for the first instance of " </Connector>" string found in the file.
Expected Output:
Unfortunately, I just know how to add the text after the string is found using sed '/pattern/a some text here' filename
I'm using bash shell script on Redhat Linux 7.
Requesting suggestions.
RudiC
2
Difficult to achieve with sed
as it can't keep the state of processing. This would insert the string after all occurrences:
sed -e "/<\/Connector/,/-->/{/-->/a $addthisline" -e'}' file
But, my requirement is to add only after the first occurance. A non sed solution will do.
RudiC
4
Try this awk
code snippet, then:
awk -v"IT=${addthisline}" '/<\/Connector/,/-->/ {if (/-->/ && !L) {print; $0 = IT; L = 1}} 1' file
And, playing around with sed
's hold space, I found this which might do as well:
sed -e "/<\/Connector/,/-->/{/-->/{x; s/XXX/&/; x; t; a $addthisline" -e'; x; s/.*/XXX/; x;} }' file