I have a xml input file which contains below
INPUT FILE
<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>
I want to search with "Total Invoices Released" and add some text after 4 lines of the matching pattern from file.txt. Similiarly i want to search "Total Deep Invoice Released" and add some text after 4 lines of the matching pattern from file.txt. Th eoutput will be like below
I want an Output file like below
OUTPUT FILE
<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>
I have coded but it is not giving me the expected output.
CODE
#!/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
The input file is like below
INPUT FILE
C71 This is He
C72 This is She
Can someone tell me whats wrong with the code
-
You should tell us. What happens when you run your code?pLumo– pLumo2019年08月26日 11:23:56 +00:00Commented Aug 26, 2019 at 11:23
-
is this for spamming/scamming?cas– cas2019年08月27日 01:31:41 +00:00Commented Aug 27, 2019 at 1:31
1 Answer 1
Just tried to tweak your existing code rather than trying new... Try this,
#!/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
- keep the counter ticking and add to the line number, since the line number will increase in every append
Output:
<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>