我有一个 XML 文件,其中包含以下内容
<SummaryRecordMapping>
<eName>Licensed Original MC TPE EXCESSIVE AUTH</eName>
<jobs>
<job>
<SummaryRecordMapping>
<eName>Licensed Reversal MC TPE EXCESSIVE AUTH</eName>
<jobs>
<job>
其中 , <eName>Licensed Original
, </eName>
,<eName>Licensed Reversal
是静态的。
我想要如下输出
<SummaryRecordMapping>
<eName>Licensed Original MC This is New Deep</eName>
<jobs>
<job>
<SummaryRecordMapping>
<eName>Licensed Reversal MC This is New Deep</eName>
<jobs>
<job>
<SummaryRecordMapping>
<eName>Licensed Original MC This is Mayurika</eName>
<jobs>
<job>
<SummaryRecordMapping>
<eName>Licensed Reversal MC This is Mayurika</eName>
<jobs>
<job>
下面是我的代码
#!/bin/bash
while read whole_line ;
do
name=`echo "$whole_line" | awk '{$1=""; print}'`
LO="<eName>Licensed Original MC ${name}</eName>"
awk -v diff="$LO" '{ if(NR==2) { print diff}
else {print $0} } ' southBalanceRecon.xml >>LO.xml
LR="<eName>Licensed Reversal MC ${name}</eName>"
awk -v diff="$LR" '{ if(NR==14) { print diff}
else {print $0} } ' LO.xml >> LR.xml
done < file.txt
我的 file.txt 包含以下内容
C71 This is New Deep
C72 This is Mayurika
当我尝试运行我的代码时,我没有得到上面提到的可能输出。谁能告诉我我的代码有什么问题吗?
答案1
尝试这个,
while IFS= read -r line; do
NAME="`echo "$line" | awk '{$1=""; print}'`"
sed "s#^<eName>Licensed Original.*</eName>#<eName>Licensed Original$NAME</eName>#;s#^<eName>Licensed Reversal.*</eName>#<eName>Licensed Reversal$NAME</eName>#" LO.xml >> LR.xml
done < input.txt
答案2
您对这个解决方案感觉如何:
sed -i -E -e "s/(Original|Reversal) .*</\1$(sed '1q;d' file.txt)</" -e 's/C[0-9]*//' file.xml
text="$(cat foo)" && echo "$text" \
| sed -E -e "s/(Original|Reversal) .*</\1$(sed '2q;d' file.txt)</" -e 's/C[0-9]*//' >> file.xml
找到该模式,Origina|Reversal
直到<
sed -i -E -e "s/(Original|Reversal) .*</
替换为第一行file.txt
/\1$(sed '1q;d' file.txt)</" -e 's/C[0-9]*//' file.xml
复制 xml 文件的内容并重复该操作,这次使用第二行file.txt
并将输出附加到 xml 文件
text="$(cat foo)" && echo "$text" \
| sed -E -e "s/(Original|Reversal) .*</\1$(sed '2q;d' file.txt)</" -e 's/C[0-9]*//' >> file.xml