我有一个来自我们的应用程序服务器的转储,其中包含多个字符串的 XML。我对 userID 感兴趣,它嵌入在 XML 标签中,格式为 (lasfir1),如下面的 XML 示例所示:
<row>
<string></string>
<integer>2177</integer>
<string>assignee =lasfir1 </string>
<string>Firstname Lastname</string>
<integer>10</integer>
<string xsi:nil="true"/>
<integer>450</integer>
</row>
<row>
<string>#ffd600</string>
<integer>2199</integer>
<integer>23</integer>
<integer>474</integer>
<string>assignee</string>
<string>lasfir1</string>
</row>
<row>
<integer>1536</integer>
<string>lasfir1</string>
<integer>235</integer>
<string>USER</string>
</row>
<row>
<string>#ffd610</string>
<integer>2200</integer>
<integer>25</integer>
<integer>464</integer>
<string>assignee</string>
<string>lisfar1</string>
</row>
要求是将字符串“lasfir1”仅转换为其等效的电子邮件 ID,该 ID 可在另一个 CSV(文本)文件中使用,该文件具有用户 ID 和电子邮件 ID 的键->值配对:
[email protected],lasfir1
[email protected],lisfar1
[email protected],firlas1
XML 可能并不总是相同的,但要搜索的是字符串,而不是它前面或后面的模式。
是否有一些简单的方法可以读取键-> 值对(在 CSV 文件中),检查 XML 文件中是否存在键(用户 ID),然后将其替换为“值”(电子邮件 ID)
这是一组 300 多个用户 ID 和电子邮件 ID 组合所必需的,所有这些组合可能都不在 XML 中。
看看这个 Perl one liner 解决方案:
$ cat gagneet.csv
[email protected],lasfir1
[email protected],lisfar1
[email protected],firlas1
$ cat gagneet.xml
<row>
<string></string>
<integer>2177</integer>
<string>assignee =lasfir1 </string>
<string>Firstname Lastname</string>
<integer>10</integer>
<string xsi:nil="true"/>
<integer>450</integer>
</row>
. . . .
. . . .
$ perl -ne 'BEGIN { %kv=map{chomp;(split(",",$_))[1,0] } qx(cat gagneet.csv) ; $content=qx(cat gagneet.xml);while($content=~/(<row>)(.*?)(<\/row>)/smg) { $xml=$2;forea
ch $y (keys %kv) { $xml=~s/${y}/$kv{$y}/gm; } print "$1$xml$3\n"; } exit } '
<row>
<string></string>
<integer>2177</integer>
<string>assignee [email protected] </string>
<string>Firstname Lastname</string>
<integer>10</integer>
<string xsi:nil="true"/>
<integer>450</integer>
</row>
<row>
<string>#ffd600</string>
<integer>2199</integer>
<integer>23</integer>
<integer>474</integer>
<string>assignee</string>
<string>[email protected]</string>
</row>
<row>
<integer>1536</integer>
<string>[email protected]</string>
<integer>235</integer>
<string>USER</string>
</row>
<row>
<string>#ffd610</string>
<integer>2200</integer>
<integer>25</integer>
<integer>464</integer>
<string>assignee</string>
<string>[email protected]</string>
</row>
如果您只想在标签之间进行编辑,那么
$ perl -ne 'BEGIN { %kv=map{chomp;(split(",",$_))[1,0] } qx(cat gagneet.csv) ; $content=qx(cat gagneet.xml);while($content=~/(<row>)(.*?)(<\/row>)/smg) { $xml=$2;forea
ch $y (keys %kv) { $xml=~s/<string>${y}<\/string>/<string>$kv{$y}<\/string>/gm; } print "$1$xml$3\n"; } exit } '
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句