使用 shell 在匹配模式的 4 行之后添加一些文本

使用 shell 在匹配模式的 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>

相关内容