Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need to fix an error and to replace the second tag </time> with ` 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

Any help?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

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>!'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.