쉘을 사용하여 패턴과 일치하는 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>

관련 정보