Thanks a lot for your reply.
I have used sed before for basic find-and-replace tasks, this one being more ambitious. That's why I'm probably missing out on certain skills or best practices in sed scripting.
Anyway, here's what I'm trying to do:
Starting with a file generated in a markup language (FrameMaker MIF), I'd like to
- take some string values (highlighted in red), and
- add the same values inside a different markup element to the original file (the whole additions highlighted in blue).
The original MIF file has 30000+ lines, so here's an excerpt with the relevant bits on which you can try out the script:
Code:
<MIFFile 8.00>
----- snip -----
<XRef
<XRefName `Navigation 4'>
<XRefSrcText `CHDEEGGJ'>
<XRefSrcIsElem Yes>
<XRefSrcFile `<c\>test2.xml'>
<XRefLastUpdate 1263890843 26000>
<Unique 1007728>
<Element
<Unique 1007731>
<ETag `xref'>
<Attributes
<Attribute
<AttrName `IDREF'>
<AttrValue `CHDEEGGJ'>
> # end of Attribute
> # end of Attributes
<Collapsed No>
<SpecialCase No>
<AttributeDisplay ReqAndSpec>
> # end of Element
> # end of XRef
----- snip -----
<XRef
<XRefName `Heading & Page'>
<XRefSrcText `CHDHFFJF: cname: 1.1.1.2 Sample Heading'>
<XRefSrcIsElem Yes>
<XRefSrcFile `<c\>test2.xml'>
<XRefLastUpdate 1263801131 424000>
<Unique 1011275>
<Element
<Unique 1011278>
<ETag `xref'>
<Attributes
<Attribute
<AttrName `IDREF'>
<AttrValue `CHDHFFJF'>
> # end of Attribute
> # end of Attributes
<Collapsed No>
<SpecialCase No>
<AttributeDisplay ReqAndSpec>
> # end of Element
> # end of XRef
----- snip -----
# EOF
The intended result
should look like this, however, I get only the first of the two blocks highlighted in blue:
Code:
<MIFFile 8.00>
----- snip -----
<Marker
<MType 12>
<MTypeName `UnstructXRef'>
<MText `;;Navigation 4;;CHDEEGGJ;;test2.xml;;' >
> # end of Marker
<XRef
<XRefName `Navigation 4'>
<XRefSrcText `CHDEEGGJ'>
<XRefSrcIsElem Yes>
<XRefSrcFile `<c\>test2.xml'>
<XRefLastUpdate 1263890843 26000>
<Unique 1007728>
<Element
<Unique 1007731>
<ETag `xref'>
<Attributes
<Attribute
<AttrName `IDREF'>
<AttrValue `CHDEEGGJ'>
> # end of Attribute
> # end of Attributes
<Collapsed No>
<SpecialCase No>
<AttributeDisplay ReqAndSpec>
> # end of Element
> # end of XRef
----- snip -----
<Marker
<MType 12>
<MTypeName `UnstructXRef'>
<MText `;;Heading & Page;;CHDHFFJF: cname: 1.1.1.2 Sample Heading;;test2.xml;;' >
> # end of Marker
<XRef
<XRefName `Heading & Page'>
<XRefSrcText `CHDHFFJF: cname: 1.1.1.2 Sample Heading'>
<XRefSrcIsElem Yes>
<XRefSrcFile `<c\>test2.xml'>
<XRefLastUpdate 1263801131 424000>
<Unique 1011275>
<Element
<Unique 1011278>
<ETag `xref'>
<Attributes
<Attribute
<AttrName `IDREF'>
<AttrValue `CHDHFFJF'>
> # end of Attribute
> # end of Attributes
<Collapsed No>
<SpecialCase No>
<AttributeDisplay ReqAndSpec>
> # end of Element
> # end of XRef
----- snip -----
Finally, here is the original sed script from my input file:
Code:
/<XRef\s$/ {
h
n
# 2
H
n
# 3
H
n
# 4
H
n
# 5
H
g
s/\(.*\s<XRef.*\s<XRefName\s`\([A-z0-9 ]*\)'>.*\s<XRefSrcText\s`\([A-z0-9 ]*\)'>.*\s<XRefSrcFile\s`\([A-z0-9<>\\ ]*\)'>\)/<Marker\n<MType 12>\n<MTypeName `UnstructXRef'>\n<MText ;;\2;;\3;;\4;; >\n> \# end of Marker\n\1/
}
p
Leading whitespace is not critical in the output.
I find it convenient using an input file but would be just as happy with a working command line script. I've already changed the first H to lowercase and removed removed the line for cleaing the hold buffer.
Thanks again for listening. Maybe you could give me some more hints how to proceed?
jk