>我有一个csv文件,在另一行的开头都有一个文本字符串,在另一个长XML字符串开始之前。下面是一行,为了方便起见,我将其格式化为 XML。
0b51b828-9416-4933-80ad-dd44ae2377b5<Company xmlns="abcd">
<Employee>
<Id>999999</Id>
<Name>Hulk</Name>
<Email>hulk@smash.com</Email>
</Employee>
<ApplicationName/>
<Identifier/>
<Headquarter>
<City>XYZ</City>
<House>123</House>
</Headquarter>
</Company>
我需要提取起始文本,直到 xml 开始的"<"首次出现并修改每一行,如下所示:
<Record> -- adding parent xml enclosure
<Parent_id>0b51b828-9416-4933-80ad-dd44ae2377b5</Parent_id> -- for reference
<Company xmlns="abcd">
<Employee>
<P_id>0b51b828-9416-4933-80ad-dd44ae2377b5</P_id> -- replicating p_id under each xml tag groups
<Id>999999</Id>
<Name>Hulk</Name>
<Email>hulk@smash.com</Email>
</Employee>
<ApplicationName/>
<Identifier/>
<Headquarter>
<P_id>0b51b828-9416-4933-80ad-dd44ae2377b5</P_id> -- same here
<City>XYZ</City>
<House>123</House>
</Headquarter>
</Company>
</Record>
我假设它需要多次迭代才能实现这一目标,但我对任何想法都持开放态度。可以使用的工具是shell,map reduce或任何在文件的每一行上执行此操作的有效方法。
谢谢!
给定:
$ cat file
0b51b828-9416-4933-80ad-dd44ae2377b5<Company xmlns="http://example.com/abcd"><Employee><Id>999999</Id><Name>Hulk</Name><Email>hulk@smash.com</Email></Employee><ApplicationName/><Identifier/><headquarter><city>XYZ</city><house>123</house></headquarter></Company>
然后:
IFS='<' read -r string xml < file
xml="<$xml" # add the leading bracket that the read command removed.
{
echo "<Record>"
xmlstarlet edit --omit-decl
--subnode /_:Company/_:Employee --type elem --name P_id --value "$string"
--subnode /_:Company/_:headquarter --type elem --name P_id --value "$string"
--subnode / --type elem --name Parent_id --value "$string"
<<<"$xml"
echo "</Record>"
}
输出
<Record>
<Company xmlns="http://example.com/abcd">
<Employee>
<Id>999999</Id>
<Name>Hulk</Name>
<Email>hulk@smash.com</Email>
<P_id>0b51b828-9416-4933-80ad-dd44ae2377b5</P_id>
</Employee>
<ApplicationName/>
<Identifier/>
<headquarter>
<city>XYZ</city>
<house>123</house>
<P_id>0b51b828-9416-4933-80ad-dd44ae2377b5</P_id>
</headquarter>
</Company>
<Parent_id>0b51b828-9416-4933-80ad-dd44ae2377b5</Parent_id>
</Record>