我有一个由多个非常相似的块组成的 XML 文件。这里有两个:
<Grid Name="EMFieldMany" GridType="Uniform">
<Topology TopologyType="3DRectMesh" Dimensions="40 40 40 "/>
<Attribute AttributeType="Scalar" Name="Er" Center="Node">
<DataItem Dimensions="40 40 40 " NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
Field_reflected_time_3D.hdf5:/field/Er-0
</DataItem>
</Attribute>
<Geometry GeometryType="VXVYVZ">
<DataItem Name="r" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
Field_reflected_time_3D.hdf5:/coordinates/r
</DataItem>
<DataItem Name="theta" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
Field_reflected_time_3D.hdf5:/coordinates/theta
</DataItem>
<DataItem Name="z" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
Field_reflected_time_3D.hdf5:/coordinates/z
</DataItem>
</Geometry>
</Grid>
<Grid Name="EMFieldMany" GridType="Uniform">
<Topology TopologyType="3DRectMesh" Dimensions="40 40 40 "/>
<Attribute AttributeType="Scalar" Name="Er" Center="Node">
<DataItem Dimensions="40 40 40 " NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
Field_reflected_time_3D.hdf5:/field/Er-1
</DataItem>
</Attribute>
<Geometry GeometryType="VXVYVZ">
<DataItem Name="r" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
Field_reflected_time_3D.hdf5:/coordinates/r
</DataItem>
<DataItem Name="theta" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
Field_reflected_time_3D.hdf5:/coordinates/theta
</DataItem>
<DataItem Name="z" Dimensions="40" NumberType="Float" Precision="8" Format="HDF5" Endian="Big">
Field_reflected_time_3D.hdf5:/coordinates/z
</DataItem>
</Geometry>
</Grid>
<Grid>
通常我在一个文件中有数百个类似的对象。现在,我想以编程方式交换每个对象中的<DataItem Name="r">
和<DataItem Name="z">
块的位置,以使s 的顺序为 z、theta、r。此外,对于每个语句,每个包含三个值的属性,我希望将属性重写为.<Grid>
<DataItem>
Dimensions=" x y z "
Dimensions
Dimensions=" z y x "
我真的不介意用于执行此操作的编程语言。我在一个带有 bash、python、perl 的 Linux 工作站上......所有标准的东西。
编辑:此答案用于sed
匹配文本块,但我不确定之后如何操作选定的块。这个其他答案上下交换单行,但我不确定如何概括为文本块,并使其交换块。