bashスクリプトには新しく、csvデータファイルからXML文書を生成しようとしています。私が扱っているサンプルデータファイルには、要素ごとに5つの属性を持つ3つの要素(式列)が含まれています(数百もあります)。ネストされたループがありますが、最初のループに戻る前に5つの属性を完了するために2番目のループを取得できません。bashスクリプトさまざまな数の属性を持つxml要素を作成するための入力ファイル(csv)のネストされたループ
私のサンプルデータファイルはこれです:
source,attribute,type,expression,confidence
profile_sysObjectID,FastIron ,Model,^.?1\.3\.6\.1\.4\.1\.1991\.1\.3\.1,92
profile_sysObjectID,Switch/Router,DeviceType,^.?1\.3\.6\.1\.4\.1\.1991\.1\.3\.1,90
profile_sysObjectID,Brocade,Vendor,^.?1\.3\.6\.1\.4\.1\.1991\.1\.3\.1,90
profile_sysObjectID,Embedded (Brocade),OS,^.?1\.3\.6\.1\.4\.1\.1991\.1\.3\.1,90
profile_sysObjectID,Embedded (Brocade),Version,^.?
1\.3\.6\.1\.4\.1\.1991\.1\.3\.1,90
profile_sysObjectID,FastIron WG Switch,Model,^.?
1\.3\.6\.1\.4\.1\.1991\.1\.3\.1\.1,92
profile_sysObjectID,Switch,DeviceType,^.?1\.3\.6\.1\.4\.1\.1991\.1\.3\.1\.1,92
profile_sysObjectID,Brocade,Vendor,^.?1\.3\.6\.1\.4\.1\.1991\.1\.3\.1\.1,92
profile_sysObjectID,Embedded (Brocade),OS,^.?
1\.3\.6\.1\.4\.1\.1991\.1\.3\.1\.1,92
profile_sysObjectID,Embedded (Brocade),Version,^.?
1\.3\.6\.1\.4\.1\.1991\.1\.3\.1\.1,92
profile_sysObjectID,FastIron BB Switch,Model,^.?
1\.3\.6\.1\.4\.1\.1991\.1\.3\.1\.2,92
profile_sysObjectID,Switch,DeviceType,^.?1\.3\.6\.1\.4\.1\.1991\.1\.3\.1\.2,92
profile_sysObjectID,Brocade,Vendor,^.?1\.3\.6\.1\.4\.1\.1991\.1\.3\.1\.2,92
profile_sysObjectID,Embedded (Brocade),OS,^.?
1\.3\.6\.1\.4\.1\.1991\.1\.3\.1\.2,92
profile_sysObjectID,Embedded (Brocade),Version,^.?
1\.3\.6\.1\.4\.1\.1991\.1\.3\.1\.2,92
私の現在のスクリプトは次のようになります。このスクリプトサブループのみの最初の属性から
#!/bin/bash
echo ' input filename...'
read file_in
file_out="sampleout.xml"
echo '<patterns xmlns="urn:lumeta:pattern:6.0" version="2.1.666" user-
provided="true">' > $file_out
while IFS=$',' read -r -a arry
do
echo ' <pattern>' >> $file_out
echo ' <source>'${arry[0]}'</source>' >> $file_out
echo ' <expression>'${arry[3]}'</expression>' >> $file_out
echo ' <attributes>' >> $file_out
exp=${arry[3]}
for exp in ${arry[3]}
do
echo ' <attribute> type="'${arry[2]}'"
confidence="'${arry[4]}'">'${arry[1]}'</attribute>' >> $file_out
done
echo ' </pattern>' >> $file_out
done < $file_in
echo '</patterns>' >> $file_out
exit 0
私の現在のXML出力新しい要素を作成するために最初のループに戻る前に親要素。
電流出力は次のようになります。
<patterns xmlns="urn:lumeta:pattern:6.0" version="2.1.666" user-provided="true">
<pattern>
<source>profile_sysObjectID</source>
<expression>^.?1\.3\.6\.1\.4\.1\.1991\.1\.3\.1</expression>
<attributes>
<attribute> type="Model" confidence="92">FastIron </attribute>
</pattern>
<pattern>
<source>profile_sysObjectID</source>
<expression>^.?1\.3\.6\.1\.4\.1\.1991\.1\.3\.1</expression>
<attributes>
<attribute> type="DeviceType" confidence="90">Switch/Router</attribute>
</pattern>
<pattern>
<source>profile_sysObjectID</source>
<expression>^.?1\.3\.6\.1\.4\.1\.1991\.1\.3\.1</expression>
<attributes>
<attribute> type="Vendor" confidence="90">Brocade</attribute>
</pattern>
<pattern>
<source>profile_sysObjectID</source>
<expression>^.?1\.3\.6\.1\.4\.1\.1991\.1\.3\.1</expression>
<attributes>
<attribute> type="OS" confidence="90">Embedded (Brocade)</attribute>
</pattern>
<pattern>
<source>profile_sysObjectID</source>
<expression>^.?1\.3\.6\.1\.4\.1\.1991\.1\.3\.1</expression>
<attributes>
<attribute> type="Version" confidence="90">Embedded (Brocade)</attribute>
</pattern>
<pattern>
<source>profile_sysObjectID</source>
<expression>^.?1\.3\.6\.1\.4\.1\.1991\.1\.3\.1\.1</expression>
<attributes>
<attribute> type="Model" confidence="92">FastIron WG Switch</attribute>
</pattern>
<pattern>
<source>profile_sysObjectID</source>
<expression>^.?1\.3\.6\.1\.4\.1\.1991\.1\.3\.1\.1</expression>
<attributes>
<attribute> type="DeviceType" confidence="92">Switch</attribute>
</pattern>
<pattern>
<source>profile_sysObjectID</source>
<expression>^.?1\.3\.6\.1\.4\.1\.1991\.1\.3\.1\.1</expression>
<attributes>
<attribute> type="Vendor" confidence="92">Brocade</attribute>
</pattern>
<pattern>
<source>profile_sysObjectID</source>
<expression>^.?1\.3\.6\.1\.4\.1\.1991\.1\.3\.1\.1</expression>
<attributes>
<attribute> type="OS" confidence="92">Embedded (Brocade)</attribute>
</pattern>
<pattern>
<source>profile_sysObjectID</source>
<expression>^.?1\.3\.6\.1\.4\.1\.1991\.1\.3\.1\.1</expression>
<attributes>
<attribute> type="Version" confidence="92">Embedded (Brocade)</attribute>
</pattern>
上記の最初の要素のための所望の出力はこのようになります:
<patterns xmlns="urn:lumeta:pattern:6.0" version="2.1.666" user-provided="true">
<pattern>
<source>profile_sysObjectID</source>
<expression>^.?1\.3\.6\.1\.4\.1\.1991\.1\.3\.1</expression>
<attributes>
<attribute type="Model" confidence="92">FastIron </attribute>
<attribute type="DeviceType" confidence="90">Switch/Router</attribute>
<attribute type="Vendor" confidence="90">Brocade</attribute>
<attribute type="OS" confidence="90">Embedded (Brocade)</attribute>
<attribute type="Version" confidence="90">Embedded (Brocade)</attribute>
</attributes>
</pattern>
<pattern>
<source>profile_sysObjectID</source>
<expression>^.?1\.3\.6\.1\.4\.1\.1991\.1\.3\.1\.1</expression>
<attributes>
<attribute type="Model" confidence="92">FastIron WG Switch</attribute>
<attribute type="DeviceType" confidence="92">Switch</attribute>
<attribute type="Vendor" confidence="92">Brocade</attribute>
<attribute type="OS" confidence="92">Embedded (Brocade)</attribute>
<attribute type="Version" confidence="92">Embedded (Brocade)</attribute>
</attributes>
</pattern>
</patterns>
ワウ!私を正しい方向に向かわせてくれてありがとう。私は最後のカップル時間RTFD'ingあなたのコードを費やして、私はそこに着いている。 –