开发者

mass rename with sed

开发者 https://www.devze.com 2023-03-29 23:39 出处:网络
I have to work with a huge XML-File which was exported from Excel. The file looks like this: <Row>

I have to work with a huge XML-File which was exported from Excel.

The file looks like this:

<Row>
 <Data>some data..</Data>
 <Data>some data..</Data>
 <Data>some data..</Data>
 <Data>some data..</Data>
 <Data>some data..</Data>
 <Data>some data..</Data>
 <Data>some data..</Data>
<Row>

There are about 2000 Row-elements.

So there is always one Row-tag with 7 Data-subtags. Now I'd like to rename every first Data-tag to 'one', the second one to 'second' and so on..

What's the correct sed syntax to do this开发者_如何学C?


Consider using awk instead...

BEGIN {
    NUM[1]="one"
    NUM[2]="two"
    NUM[3]="three"
    NUM[4]="four"
    NUM[5]="five"
    NUM[6]="six"
    NUM[7]="seven"
}

/<Row/{
    print
    for(i=1;i<8;i++) {
        getline
        sub(/Data/, NUM[i]);print
    }
}
/<\/Row/{print}

Output:

$ awk -f r.awk input 
<Row>
 <one>some data..</one>
 <two>some data..</two>
 <three>some data..</three>
 <four>some data..</four>
 <five>some data..</five>
 <six>some data..</six>
 <seven>some data..</seven>
</Row>
0

精彩评论

暂无评论...
验证码 换一张
取 消