在使用awk /从另一个文件用绳子sed的XML文件替换字符串 [英] Replace string in XML file with awk/sed with string from another file

查看:213
本文介绍了在使用awk /从另一个文件用绳子sed的XML文件替换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉的长/怪异的标题,但我被困在一个问题,我有。我有这个XML文件:

 < /成员>
     <&成员GT;
      <名称>&的TransactionID LT; /名称>
      < VALUE><串GT; 123456789123456< /串>< /值>
     < /成员>
     <&成员GT;
      <名称>&号LT; /名称>
      < VALUE><串GT; 765101293< /串>< /值>
     < /成员>

有,我有另一个文件的另一个值替换765101293,文件2:

  765003448
765885388
7640341​​43
784478101
762568592
769765134
767200702
769550613
784914007
762333840

所以,XML文件会从文件2每个新的字符串改变。我不知道如何做到这一点。我想使用awk第一处理文件2,然后的管子喂养到sed的:

code:

 #! /斌/ bash的-fVAR =`awk的-F,\\ n'{打印$ 0}'$ 1`
SED -i/ 7 [0-9] {8} / C \\ $ VAR /$ 2

所以,我想更新与file2的数据我的XML文件(Update.xml)。对于文件2的第一行,我将不得不更新现有的XML:而不是765101293我会放文件2> 765003448.第一线然后,我将移动XML别的地方,然后我再传给上file2的下一行。重复该过程文件2的所有行。这两个变量$ 1和$ 2我以前当我运行我的脚本是这样的:./script.sh文件2 Update.xml

谁能帮助我?

编辑:

下面是我的完整的XML文件:

 < methodCall>
 <&methodName中GT; UpdateOffer< /方法名>
 <&PARAMS GT;
  <&参数GT;
   < VALUE>
    <&结构GT;
     <&成员GT;
      <名称>&OFFERID LT; /名称>
      < VALUE>< 0-14> 19 LT; / I4>< /值>
     < /成员>
     <&成员GT;
<名称>&的startDate LT; /名称>
< VALUE>< dateTime.iso8601> 20151028T14:11:00 + 0200< /dateTime.iso8601>< /值>
< /成员>
     <&成员GT;
      <名称>&offerType LT; /名称>
      < VALUE>< 0-14> 0℃; / I4>< /值>
     < /成员>
     <&成员GT;
      <名称>&originHostName LT; /名称>
      < VALUE><串GT;测试与LT; /串>< /值>
     < /成员>
     <&成员GT;
      <名称>&originNodeType LT; /名称>
      < VALUE><串GT; prepaid< /串>< /值>
     < /成员>
     <&成员GT;
      <名称>&originOperatorID LT; /名称>
      < VALUE><串GT;运营商LT; /串>< /值>
     < /成员>
<&成员GT;<名称>&originTimeStamp LT; /名称>
< VALUE>< dateTime.iso8601> 20151028T14:16:36 + 0200< /dateTime.iso8601>< /值>
< /成员>
     <&成员GT;
      <名称>&originTransactionID LT; /名称>
      < VALUE><串GT; 123456789123456< /串>< /值>
     < /成员>
     <&成员GT;
      <名称>&订户号LT; /名称>
      < VALUE><串GT; 765101293< /串>< /值>
     < /成员>
    < /结构>
   < /值>
  < /参数>
 < / PARAMS>
< / methodCall>


解决方案

这听起来像所有你需要的是:

 的awk'NR == FNR {NUMS [NR] = $ 0;接下来}子(/> 7 [0-9] {8}< />中NUMS第[i + 1]<){我++}'文件2 Update.xml

但很难猜测给出的小样本XML文件输入/输出你的问题。

请记住这一点:


  1. 每次你写壳一个循环处理文本时,你有错误的做法。见<一href=\"http://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice\">http://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice.

  2. shell是从中操纵文件和进程和顺序调用工具的环境。 UNIX的工具来处理文本是AWK。读了这本书有效AWK编程,第4版,由阿诺德·罗宾斯。

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file:

</member>
     <member>
      <name>TransactionID</name>
      <value><string>123456789123456</string></value>
     </member>
     <member>
      <name>Number</name>
      <value><string>765101293</string></value>
     </member>

There, I have to replace the "765101293" with another value from another file, file2:

765003448
765885388
764034143
784478101
762568592
769765134
767200702
769550613
784914007
762333840

So, the XML file will change at each new string from file2. I am not sure how to do this. I am thinking of first processing file2 with awk and then a pipe feeding into sed:

Code:

#! /bin/bash -f

var=`awk -F,"\n" '{print $0}' "$1"`
sed -i "/7[0-9]{8}/c\$var/" "$2"

So, I would like to update my XML file (Update.xml) with the data from file2. For the first line of file2, I will have to update the existing XML: instead of "765101293" i'll put the first line of file2 > 765003448. Then I'll move the xml somewhere else, and then I'll pass to the next line on file2. Repeat the process for all lines of file2. The two variables $1 and $2 I used when I run my script like this: ./script.sh file2 Update.xml.

Could anyone help me out?

EDIT:

Here is my full xml file:

<methodCall>
 <methodName>UpdateOffer</methodName>
 <params>
  <param>
   <value>
    <struct>
     <member>
      <name>offerID</name>
      <value><i4>19</i4></value>
     </member>
     <member>
<name>startDate</name>
<value><dateTime.iso8601>20151028T14:11:00+0200</dateTime.iso8601></value>
</member>
     <member>
      <name>offerType</name>
      <value><i4>0</i4></value>
     </member>
     <member>
      <name>originHostName</name>
      <value><string>TEST</string></value>
     </member>
     <member>
      <name>originNodeType</name>
      <value><string>Prepaid</string></value>
     </member>
     <member>
      <name>originOperatorID</name>
      <value><string>operator</string></value>
     </member>
<member><name>originTimeStamp</name>
<value><dateTime.iso8601>20151028T14:16:36+0200</dateTime.iso8601></value>
</member>
     <member>
      <name>originTransactionID</name>
      <value><string>123456789123456</string></value>
     </member>
     <member>
      <name>subscriberNumber</name>
      <value><string>765101293</string></value>
     </member>
    </struct>
   </value>
  </param>
 </params>
</methodCall>

解决方案

It sounds like all you need is:

awk 'NR==FNR{nums[NR]=$0;next} sub(/>7[0-9]{8}</,">"nums[i+1]"<"){i++}' file2 Update.xml

but it's hard to guess given the small sample XML file input/output in your question.

Please remember this:

  1. Every time you write a loop in shell to manipulate text you have the wrong approach. See http://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice.
  2. A shell is an environment from which to manipulate files and processes and sequence calls to tools. The UNIX tool to manipulate text is awk. Read the book Effective Awk Programming, 4th Edition, by Arnold Robbins.

这篇关于在使用awk /从另一个文件用绳子sed的XML文件替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆