入力ファイルとxmlファイルがあります。
入力ファイル -
/user/sht
227_89,45_99
/user/sht1
230_90
/user/sht2
441_50
ファイルにはパスと場所を含む代替行があります。
xmlファイル -
<aaa><command name="move">
<domain>
<path></path>
<positions></positions>
</domain>
<domain>
<path></path>
<positions></positions>
</domain>
<domain>
<path></path>
<positions></positions>
</domain>
</command>
</aaa>
次の必須出力xmlを提供するスクリプトを作成したら、次のxmlを入力として使用していくつかのコマンドを実行する必要があります。
<aaa><command name="move">
<domain>
<path>/user/sht</path>
<positions>227_89,45_99</positions>
</domain>
<domain>
<path>/user/sht1</path>
<positions>230_90</positions>
</domain>
<domain>
<path>/user/sht2</path>
<positions>441_50</positions>
</domain>
</command>
</aaa>
入力ファイルから1行ずつ抽出してxmlに入れてみましたが、問題はすべてのエントリが<path>
最初の入力行に置き換えられることです。
GNU bashの使用4.1.2。
ベストアンサー1
GNUsed
スクリプトを使用すると、次のことができます。
sed -n '/<aaa>/,/<.aaa>/!{H;d}
G;:a
s_>\(</path>.*\n\)\n\([^\n]*\)_>\2\1_
s_>\(</positions>.*\n\)\n\([^\n]*\)_>\2\1_;
ta;P;s/.*\n\n/\n/;h' input.txt input.xml
最初の行は、保存バッファー内の最初のファイルのすべての行を収集します。
その後、2番目のファイルの各行に保持バッファが追加されますG
。その場合は、2 つのコマンドのいずれかをpath
使用して、ラベル内の保持バッファの最初のセグメントを移動します。positions
s
いずれの場合も、その行は印刷されP
()削除され(s/.*\n\n/\n/
)、置換リストの残りの部分は次のサイクル(h
)のためにストレージバッファに戻されます。