文件名为types.xml,并列出了数百个不同的项。
看上去是这样的:
<type name="CanisterGasoline">
<nominal>50</nominal>
<lifetime>28800</lifetime>
<restock>0</restock>
<min>30</min>
<quantmin>-1</quantmin>
<quantmax>-1</quantmax>
<cost>100</cost>
<flags count_in_cargo="0" count_in_hoarder="0" count_in_map="1" count_in_player="0" crafted="0" deloot="0"/>
<category name="tools"/>
<tag name="shelves"/>
<usage name="Industrial"/>
</type>
<type name="Canteen">
<nominal>20</nominal>
<lifetime>14400</lifetime>
<restock>0</restock>
<min>10</min>
<quantmin>10</quantmin>
<quantmax>90</quantmax>
<cost>100</cost>
<flags count_in_cargo="0" count_in_hoarder="0" count_in_map="1" count_in_player="0" crafted="0" deloot="0"/>
<category name="food"/>
<usage name="Military"/>
</type>基本上,无论它写的是<nominal>20</nominal>,我都想将<nominal>和</nominal>之间的数字更改为0。
谢谢你抽出时间来!
发布于 2022-06-20 02:02:29
如果你想用sed来做这件事,你可以这样做:
sed 's@<nominal>[0-9]*</nominal>@<nominal>0</nominal>@' types.xml > output.xml这将替换<nominal>,后面是任何数字序列,后面是</nominal>,后面跟着<nominal>0</nominal>。
但是,我相信很快就会有人提出为什么您应该使用专用的XML解析器来完成这个任务,并建议使用哪一个解析器和如何使用XML解析器。
发布于 2022-06-20 06:56:04
或者使用regex's的“反向引用”,比如
sed 's@\(<nominal>\)[0-9]*\(</nominal>\)@\10\2@' types.xmlhttps://unix.stackexchange.com/questions/706779
复制相似问题