开发者

transform using a shell script a formated xml file into a one-line file containing all the tags

I have this xml file:

<content>
     <tag1>
          <innertag1>foo</innertag1&g开发者_运维知识库t;
          <innertag2>baa</innertag2>
     </tag>
     <tag2>
          <innertag1>foo2</innertag1>
          <innertag2>baa1</innertag2>
     </tag2>
</content>

and I need, using a script, another file with the same info but in one single line:

<content><tag1><innertag1>foo</innertag1><innertag2>baa</innertag2></tag><tag2><innertag1>foo 2</innertag1><innertag2>baa 1</innertag2></tag2></content>

because I need this format to use grep and sed commands. How can I do it?

thanks


tr -d '\n\t' inputfile > temp && echo >> temp && mv temp inputfile

or

sed -in ':a;$s/[\n\t]//g;N;ba' inputfile

for picky versions of sed:

sed -n  -e ':a' -e '$s/[\n\t]//g' -e 'N' -e 'ba' inputfile > temp && echo >> temp && mv temp inputfile

another option:

perl -i -pe 'chomp unless eof; s/\t//g' inputfile

or possibly:

perl -pe 'chomp unless eof; s/\t//g' inputfile > temp && echo >> temp && mv 

Edited to also remove tabs from the input file.


What about plain old regular expressions? s/>\s*</></g?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜