Sed 命令将文件中所有 xml 标记值中的“(双引号)替换为”和 '(单引号)替换为 '

问题描述 投票:0回答:1
<?xml version="1.0" encoding="UTF-8"?> <Document> <InnerDoc> <GrpHdr> <MsgId>aaa.xml</MsgId> <CreDtTm>2023-08-15T13:35:33.0Z</CreDtTm> <MsgRcpt> <Id value="111"> <OrgId> <Othr> <Id>asa-"-as'#</Id> </Othr> </OrgId> </Id> </MsgRcpt> <tag1 info = "AddInf1">Report Map = PRIOR DAY BALTRAN INCREMENTAL " - '</tag1> <tag2 info = "AddInf2">Report Map = " - '</tag1> </GrpHdr> </InnerDoc> </Document>
对于上面的xml,我需要将所有“(双引号)替换为”和'(单引号)替换为'
例如:报告地图 = PRIOR DAY BALTRAN INCRMENTAL " - &apos
它应该仅替换 xml 标记值中的文本。因此,它应该匹配模式 > 和 

之间的文本 <. could you please suggest correct sed command for this?

我尝试使用 sed 命令来替换,但它正在替换所有内容。我需要进行模式匹配并仅考虑 > 和

中的文本 < for replacing

linux sed command-line pattern-matching
1个回答
0
投票
sed -E ':a s/(>[^"]*)"(.*<)/\1'\''\2/;ta' file
XML 解析器可能更合适,但 sed 也可以做到:

    使用
  • -E
     扩展正则表达式 (ERE) 来获取不带反斜杠的捕获组
  • :a
    ta
    :循环标记 a 直到替换失败
  • s/(>[^"]*)"(.*<)/\1'\2/
     --> 不带 shell 引用/转义的替换
    
    • "
       之间时,将 
      '
      替换为
      > <
    • 非常依赖于 OP 中的 HTML 结构,这就是为什么 XML 解析器比 sed 更好
© www.soinside.com 2019 - 2024. All rights reserved.