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이 FILE2
포함 된 첫 번째 줄을 읽을 때 FS
여전히 로 설정되어 있기 때문입니다 =
.
간단한 해결책은 다음을 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]