Sed: how to replace nextline \n symbol in text files

sedtext processing

I need to fix an error and to replace the second tag </time> with </tags> in an XML file with the following structure:

<time>20260664</time>
<tags>substancesummit ss</time>
<geo>asdsadsa</geo>
<time>20260664</time>
<tags>substancesummit ss</time>
<geo>asdsadsa</geo>

I'm trying to do it using sed and since I have 2 </time> closing tag per item, my idea is to replace </time><geo> with </tags><geo>.

However there is a next line symbol in between, so I'm using \n but it doesn't work:

sed 's/time>\n<geo>/tags>\n<geo>/g' old.xml > new.xml

Best Answer

Sed processes its input line by line, so a newline character will never spontaneously appear in the input. What you could do is put lines ending in </time on hold; then if the next line begins with <geo>, do the substitution in the previous line. (This is possible in sed, using the “hold space”, but I recommend turning to awk or perl when you need the hold space.)

However, given your sample input, you can just change </time> into </tags> when the line begins with <tags>.

sed -e '/^<tags>/ s!</time>$!</tags>!'
Related Question