输入
<acc_details acct_no="00000" acct_nm="John"/>
<acc_details acct_no="00001" acct_address="109 BIRHN WAY " acct_nm="BARNS WY"/>
<acc_details acct_no="00002" acct_nm="BILL BAR" phne_nm="123456"/>
预期产出
acct_no,acct_address,acct_nm,phne_nm
00000,,John,
00001,109 BIRHN WAY,BARNS WY,
00002,,BILL BAR,123456
答案1
xml
通过添加根标签来修复文件:
<accounts>
<acc_details acct_no="00000" acct_nm="John"/>
<acc_details acct_no="00001" acct_address="109 BIRHN WAY " acct_nm="BARNS WY"/>
<acc_details acct_no="00002" acct_nm="BILL BAR" phne_nm="123456"/>
</accounts>
然后使用xml
解析器,例如xmlstarlet
:
{
echo "acct_no,acct_address,acct_nm,phne_nm"
xmlstarlet sel -t \
-m '//acc_details' \
-v "concat(@acct_no,',',@acct_address,',',@acct_nm,',',@phne_nm)" -n \
input_file
}
输出:
acct_no,acct_address,acct_nm,phne_nm
00000,,John,
00001,109 BIRHN WAY ,BARNS WY,
00002,,BILL BAR,123456