转换呼叫日志备份&将XML callist恢复为csv

时间:2016-12-23 21:32:01

标签: xml bash csv xmlstarlet

我使用

从我的所有调用文件中创建了一个大文件all.xml
echo '<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<!--File Created By Call Logs Backup & Restore v3.70 on 23/12/2016 03:02:21-->
<?xml-stylesheet type="text/xsl" href="calls.xsl"?>
<calls count="500">
'>all.xml
for i in calls-*.xml; do head -n-1 "$i"|tail -n+5; done>>all.xml
echo "</calls>">>all.xml

现在我尝试将这种格式的callist导出到csv:

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<!--File Created By Call Logs Backup & Restore v3.70 on 23/12/2016 03:02:21-->
<?xml-stylesheet type="text/xsl" href="calls.xsl"?>
<calls>
  <call number="+492345678" duration="0" date="1426694707547" type="3" readable_date="18.03.2015 17:05:07" contact_name="Someone" />
  <call number="+492345679" duration="3" date="1426695646292" type="2" readable_date="18.03.2015 17:20:46" contact_name="Someone else" />
  <call number="+492345670" duration="0" date="1426695687556" type="2" readable_date="18.03.2015 17:21:27" contact_name="Someone" />
</calls>

我尝试了xmlstarlet

xmlstarlet sel -B -t -m "//calls/call" -n -m "*" -v . -o , all.xml |less

但是这只是给我一个空列表,我猜,因为每次调用都没有值,而是每个调用元素的选项。

我无法在manuals, I found的帮助下找到答案 如何以CSV格式获取选项?

2 个答案:

答案 0 :(得分:2)

尝试将-m "*"更改为-m "@*"

答案 1 :(得分:2)

将单个xml属性转换为csv的另一种方法:

xmlstarlet sel -t -m "//calls/call" -v "concat(@number,',',@duration,',',@date,',',@type,',',@readable_date,',',@contact_name)" -n file.xml

输出:

+492345678,0,1426694707547,3,18.03.2015 17:05:07,Someone
+492345679,3,1426695646292,2,18.03.2015 17:20:46,Someone else
+492345670,0,1426695687556,2,18.03.2015 17:21:27,Someone