awk 将文件 1 中的值替换为文件 2

awk 将文件 1 中的值替换为文件 2

我有一个 xml 格式的输入文件文件1

 <Sector sectorNumber="1">
    <Cell cellNumber="1" cellCreated="YES" cellIdentity="" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
    <Cell cellNumber="2" cellCreated="YES" cellIdentity="" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
    <Cell cellNumber="3" cellCreated="YES" cellIdentity="" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
  </Sector>
  <Sector sectorNumber="2">
    <Cell cellNumber="1" cellCreated="YES" cellIdentity="" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
    <Cell cellNumber="2" cellCreated="YES" cellIdentity="" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
    <Cell cellNumber="3" cellCreated="YES" cellIdentity="" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
  </Sector>
  <Sector sectorNumber="3">
    <Cell cellNumber="1" cellCreated="YES" cellIdentity="" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
    <Cell cellNumber="2" cellCreated="YES" cellIdentity="" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
    <Cell cellNumber="3" cellCreated="YES" cellIdentity="" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
  </Sector>

和另一个文件2:

Cell11="42921"
Cell12="42925"
Cell13="42928"
Cell21="42922"
Cell22="42926"
Cell23="42929"
Cell31="42923"
Cell32="42927"
Cell33="42920"

我想从哪里替换值文件2文件1,所以它应该看起来像:

<Sector sectorNumber="1">
        <Cell cellNumber="1" cellCreated="YES" cellIdentity="42921" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
        <Cell cellNumber="2" cellCreated="YES" cellIdentity="42925" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
        <Cell cellNumber="3" cellCreated="YES" cellIdentity="42928" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
      </Sector>
      <Sector sectorNumber="2">
        <Cell cellNumber="1" cellCreated="YES" cellIdentity="42922" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
        <Cell cellNumber="2" cellCreated="YES" cellIdentity="42926" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
        <Cell cellNumber="3" cellCreated="YES" cellIdentity="42929" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
      </Sector>
      <Sector sectorNumber="3">
        <Cell cellNumber="1" cellCreated="YES" cellIdentity="42923" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
        <Cell cellNumber="2" cellCreated="YES" cellIdentity="42927" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
        <Cell cellNumber="3" cellCreated="YES" cellIdentity="42920" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
      </Sector> 

所以基本上,所有值都cellIdentity=""按顺序插入,就像文件2。我有这个 awk 代码:

awk 'FNR==NR{FS="=";a[NR]=$2;next}/cell/{c++;FS=OFS;$4="cellIdentity="a[c];}1' FILE2 FILE1

但我明白了:

<Sector sectorNumber="1">
        <Cell cellNumber "1" cellCreated "YES" cellIdentity cellIdentity= "35000" numberOfTxBranches "1" hsCodeResourceId "0"/>
<Cell cellNumber="2" cellCreated="YES" cellIdentity="42925" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
<Cell cellNumber="3" cellCreated="YES" cellIdentity="42928" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
      </Sector>
      <Sector sectorNumber="2">
<Cell cellNumber="1" cellCreated="YES" cellIdentity="42922" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
<Cell cellNumber="2" cellCreated="YES" cellIdentity="42926" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
<Cell cellNumber="3" cellCreated="YES" cellIdentity="42929" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
      </Sector>
      <Sector sectorNumber="3">
<Cell cellNumber="1" cellCreated="YES" cellIdentity="42923" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
<Cell cellNumber="2" cellCreated="YES" cellIdentity="42927" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
<Cell cellNumber="3" cellCreated="YES" cellIdentity="42920" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
</Sector>

问题只出在第一行,但是不好。我不知道如何解决它。

答案1

因为当您读取第一行包含/cell/in时FILE2FS仍然设置为=

一个简单的解决方案是split在阅读时使用FILE1

$ awk 'FNR==NR{split($0,array,"=");a[NR]=array[2];next}/cell/{FS=OFS;$4="cellIdentity="a[++c];}1' FILE2 FILE1
<Sector sectorNumber="1">
<Cell cellNumber="1" cellCreated="YES" cellIdentity="42921" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
<Cell cellNumber="2" cellCreated="YES" cellIdentity="42925" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
<Cell cellNumber="3" cellCreated="YES" cellIdentity="42928" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
  </Sector>
  <Sector sectorNumber="2">
<Cell cellNumber="1" cellCreated="YES" cellIdentity="42922" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
<Cell cellNumber="2" cellCreated="YES" cellIdentity="42926" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
<Cell cellNumber="3" cellCreated="YES" cellIdentity="42929" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
  </Sector>
  <Sector sectorNumber="3">
<Cell cellNumber="1" cellCreated="YES" cellIdentity="42923" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
<Cell cellNumber="2" cellCreated="YES" cellIdentity="42927" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
<Cell cellNumber="3" cellCreated="YES" cellIdentity="42920" cellRange="35000" numberOfTxBranches="1" hsCodeResourceId="0"/>
  </Sector>

而且你不必使用c++,你可以a[++c]直接尝试。

相关内容