シェルを使用して、パターンに一致する4行の後にテキストを追加します。

シェルを使用して、パターンに一致する4行の後にテキストを追加します。

以下を含むxml入力ファイルがあります。

入力ファイル

<Formula>
<name>Total Invoices Released</name>
<comment></comment>
<sumType>Suspense</sumType>
<operands>
<operand>Total Hui Invoice Released</operand>
<operand>Total hdrt Invoices Released</operand>
<operand>Total hgfyu Invoices Released</operand>




<name>Total Deep Invoice Released</name>
<comment></comment>
<sumType>Suspense</sumType>
<operands>
<operand>-Total Deep Licensed Original </operand>
<operand>-Total Deep Licensed Original </operand>
<operand>-Total Deep Licensed Original </operand>

「Total Invoices Released」を使って検索し、file.txtで4行のパターンを一致させた後にテキストを追加したいと思います。同様に、「Total Deep Invoice Released」を検索し、パターンに一致するfile.txtの4行の後にテキストを追加したいと思います。 e出力は次のとおりです。

次の出力ファイルが必要です。

結果ファイル

<Formula>
<name>Total Invoices Released</name>
<comment></comment>
<sumType>Suspense</sumType>
<operands>
<operand>-Total Licensed Original This is he</operand>
<operand>-Total Licensed Reversal This is he</operand>
<operand>-Total Licensed Original This is she</operand>
<operand>-Total Licensed Reversal This is she</operand>
<operand>Total Hui Invoice Released</operand>
<operand>Total hdrt Invoices Released</operand>
<operand>Total hgfyu Invoices Released</operand>




<name>Total Deep Invoice Released</name>
<comment></comment>
<sumType>Suspense</sumType>
<operands>
<operand>-Total Deep Licensed Original This is he</operand>
<operand>-Total Deep Licensed Reversal This is he</operand>
<operand>-Total Deep Licensed Original This is she</operand>
<operand>-Total Deep Licensed Reversal This is she</operand>
<operand>-Total Deep Licensed Original </operand>
<operand>-Total Deep Licensed Original </operand>
<operand>-Total Deep Licensed Original </operand>

コーディングしましたが、期待した結果は出ません。

パスワード

#!/bin/bash
IR=`grep -n "<name>Total Invoices Released</name>" BalanceForm.xml | cut -d: -f 1`
VIR=`grep -n "<name>Total Deep Invoice Released</name>" BalanceForm.xml | cut -d: -f 1`
TOTAL=`expr $IR + 4`
TOT=`expr $VIR + 4`
while IFS= read -r line; do
NAME="`echo "$line" | awk '{$1=""; print}'`"
sed -i "{
       ${TOTAL}i\<operand>-Total Licensed Original $NAME</operand>
       ${TOTAL}i\<operand>-Total Licensed Reversal $NAME</operand>
       ${TOT}i\<operand>-Total Deep Licensed Original $NAME</operand>
       ${TOT}i\<operand>-Total Deep Licensed Reversal $NAME</operand>
     }" BalanceForm.xml
done < file.txt

入力ファイルは次のとおりです

入力ファイル

C71 This is He
C72 This is She

誰かがコードにどのような問題があるのか​​教えてもらえますか?

ベストアンサー1

新しいコードを試すのではなく、既存のコードを修正してみてください。これを試してみてください。

#!/bin/bash
count=0
IR=`grep -n "<name>Total Invoices Released</name>" BalanceForm.xml | cut -d: -f 1`
VIR=`grep -n "<name>Total Deep Invoice Released</name>" BalanceForm.xml | cut -d: -f 1`
while IFS= read -r line; do
NAME="`echo "$line" | awk '{$1=""; print}'`"
TOTAL=`expr $IR + 4 + $count`
TOT=`expr $VIR + 4 + $count + $count`
sed -i "{
       ${TOTAL}i\<operand>-Total Licensed Original $NAME</operand>
       ${TOTAL}i\<operand>-Total Licensed Reversal $NAME</operand>
       ${TOT}i\<operand>-Total Deep Licensed Original $NAME</operand>
       ${TOT}i\<operand>-Total Deep Licensed Reversal $NAME</operand>
     }" BalanceForm.xml
count=$(expr $count + 2)
done < file.txt
  • 追加するたびに行番号が増加するため、カウンターを表示し続け、行番号に追加します。

出力:

<Formula>
<name>Total Invoices Released</name>
<comment></comment>
<sumType>Suspense</sumType>
<operands>
<operand>-Total Licensed Original  This is He</operand>
<operand>-Total Licensed Reversal  This is He</operand>
<operand>-Total Licensed Original  This is She</operand>
<operand>-Total Licensed Reversal  This is She</operand>
<operand>Total Hui Invoice Released</operand>
<operand>Total hdrt Invoices Released</operand>
<operand>Total hgfyu Invoices Released</operand>




<name>Total Deep Invoice Released</name>
<comment></comment>
<sumType>Suspense</sumType>
<operands>
<operand>-Total Deep Licensed Original  This is He</operand>
<operand>-Total Deep Licensed Reversal  This is He</operand>
<operand>-Total Deep Licensed Original  This is She</operand>
<operand>-Total Deep Licensed Reversal  This is She</operand>
<operand>-Total Deep Licensed Original </operand>
<operand>-Total Deep Licensed Original </operand>
<operand>-Total Deep Licensed Original </operand>

おすすめ記事