2016-12-27 5 views
0

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> 

答えて

0

私は整形式XMLにするために、ご希望の出力を編集しました。 私はたくさんのbash-ismsで宿題をしました。私はいくつかのエラーチェックコードを追加せずに本番環境でこれを使用しないことに注意してください。コメントはありませんが、RTFDのビットがあれば、すべてが十分にクリアになります。

#!/bin/bash 

function read_line() { 
    declare -g $vars 
    IFS=, read $vars || break 
} 

function tag_open() { 
    declare -g indent 
    echo "${indent}<${1}${2:+ }$2>" 
    indent+=' ' 
} 

function tag_close() { 
    indent="${indent% }" 
    echo "${indent}</$1>" 
} 

function print_tagged_var() { 
    echo "$indent<${1}${2:+ }$2>${!1}</$1>" 
} 

function print_attribute() { 
    local props 
    printf -v props 'type="%s" confidence="%u"' "$type" "$confidence" 
    print_tagged_var attribute "$props" 
} 

function consume_pattern() { 
    read_line 
    tag_open pattern 
     print_tagged_var source 
     print_tagged_var expression 
     tag_open attributes 
      print_attribute 
      for i in {1..4}; do 
       read_line 
       print_attribute 
      done 
     tag_close attributes 
    tag_close pattern 
} 


##### MAIN LOOP ##### 

read vars 
vars="${vars//,/ }" 
tag_open patterns 'xmlns="urn:lumeta:pattern:6.0" version="2.1.666" user-provided="true"' 
    while :; do 
     consume_pattern 
    done 
tag_close patterns 
+0

ワウ!私を正しい方向に向かわせてくれてありがとう。私は最後のカップル時間RTFD'ingあなたのコードを費やして、私はそこに着いている。 –

関連する問題