2010-07-23 81 views
0

我有像下面轉換XML到CSV狀結構

<Open> 
     <ID>7171</ID> 
     <Name>REENU</Name> 
     <Description>CHUMMA</Description> 
     <Active>1</Active> 
     <Users>1</Users> 
</Open> 

<Open> 
     <ID>7172</ID> 
     <Name>BEENU</Name> 
     <Description>CHUMMA</Description> 
     <Active>1</Active> 
     <Users>1</Users> 
</Open> 

<Open> 
     <ID>7173</ID> 
     <Name>CEENU</Name> 
     <Description>CHUMMA</Description> 
     <Active>1</Active> 
     <Users>1</Users> 
</Open> 

<Open> 
     <ID>7174</ID> 
     <Name>DEENU</Name> 
     <Description>CHUMMA</Description> 
     <Active>1</Active> 
     <Users>1</Users> 
</Open> 

<Open> 
     <ID>7175</ID> 
     <Name>GEENU</Name> 
     <Description>CHUMMA</Description> 
     <Active>1</Active> 
     <Users>1</Users> 
</Open> 

我想用UNIX命令,如下面使用UNIX命令轉換成CSV一個xml數據 基本上我要打印的XML的中間值標記

Open,7171,REENU,CHUMMA,1,1 
Open,7172,BEENU,CHUMMA,1,1 
Open,7173,CEENU,CHUMMA,1,1 
Open,7174,DEENU,CHUMMA,1,1 
Open,7175,GEENU,CHUMMA,1,1 
+2

請格式化你的問題。更具描述性的標題也會很棒。 – miku 2010-07-23 17:02:13

+2

[Just do it。](http://lithe.files.wordpress.com/2008/05/nike-just-do-it.jpg) – miku 2010-07-23 17:05:19

+0

這在XSLT中是微不足道的。你有XSLT處理器嗎? – 2010-07-23 20:13:53

回答

0
$ nawk '/<\/Open>/{print ""}{gsub(/<.[^>]*>/,"");printf "%s, ",$0} ' file | sed -e 's/^[ \t,]*/Open,/' -e 's/,[ \t]*/,/g' -e 's/,$//' 
Open,7171,REENU,CHUMMA,1,1 
Open,7172,BEENU,CHUMMA,1,1 
Open,7173,CEENU,CHUMMA,1,1 
Open,7174,DEENU,CHUMMA,1,1