利用sed将xml报文转换为分隔符形式报文

原始xml文本如下

 1 xml version="1.0" encoding="utf-8"?>
 2 <Message>
 3   <Header>
 4     <Version>2000000Version>
 5     <MessageClass>5MessageClass>
 6     <MessageType>7MessageType>
 7     <SenderId>9999999964020001SenderId>
 8     <ReceiverId>9999999964011001ReceiverId>
 9     <MessageId>3280260MessageId>
10   Header>
11   <Body ContentType="1">
12     <ClearTargetDate>2017-03-22ClearTargetDate>
13   <ServiceProviderId>9999999934030001ServiceProviderId>
14     <IssuerId>9999999964011001IssuerId>
15     <MessageId>406843026MessageId>
16     <Count>1Count>
17     <Amount>110.00Amount>
18     <Transaction>
19       <TransId>1TransId>
20       <Time>2017-03-21T20:40:36Time>
21       <Fee>110.00Fee>
22       <Service>
23         <ServiceType>1ServiceType>
24         <Description>曹庄|宿州Description>
25         <Detail>1|04|3401|804|33|20170321 204036|03|3401|1105|1|20170321 182056Detail>
26       Service>
27       <ICCard>
28         <CardType>22CardType>
29         <NetNo>6401NetNo>
30         <CardId>1638220100098530CardId>
31         <License>宁B63222License>
32         <TransNo>104TransNo>
33         <PreBalance>2157.60PreBalance>
34         <PostBalance>2047.60PostBalance>
35       ICCard>
36       <Validation>
37         <TAC>9439DAD2TAC>
38         <TransType>09TransType>
39         <TerminalNo>0134000030BCTerminalNo>
40         <TerminalTransNo>0018002DTerminalTransNo>
41       Validation>
42       <OBU>
43         <NetNo>C4FENetNo>
44         <OBUId>0000000200031918OBUId>
45         <OBEState>0001OBEState>
46         <License>宁B63222License>
47       OBU>
48     Transaction>
49   Body>
50 Message>

 

现在需要将上述内容Transaction标签中的值转换为下面的分隔符格式 

1|||2017-03-21T20:40:36|||110.00|||1|||曹庄|宿州|||1|04|3401|804|33|20170321204036|03|3401|1105|1|20170321182056||||||22|||6401|||1638220100098530|||宁B63222|||104|||2157.60|||2047.60||||||9439DAD2|||09|||0134000030BC|||0018002D||||||C4FE|||0000000200031918|||0001|||宁B63222|||

 

下面是我执行的操作步骤

1、替换换行符,将整个xml文件处理成一行文本,重定向到文本1中

cat ***.xml | tr "\n" " " > 1

 

结果如下

 

    2000000     5     7     9999999964020001     9999999964011001     3280260  
      2017-03-22     9999999934030001     9999999964011001     406843026     1     110.00          1             110.00               1         曹庄|宿州         1|04|3401|804|33|20170321 204036|03|3401|1105|1|20170321182056                     22         6401         1638220100098530         宁B63222         104         2157.602047.60                     9439DAD2         09      0134000030BC         0018002D              C4FE         0000000200031918         0001         宁B63222            

 

2、去除空格

sed 's/ //g' 1 > 2

 

结果如下

200000057999999996402000199999999640110013280260
2017-03-22999999993403000199999999640110014068430261110.001110.001曹庄|宿州1|04|3401|804|33|20170321204036|03|3401|1105|1|201703211820562264011638220100098530宁B632221042157.602047.609439DAD2090134000030BC0018002DC4FE00000002000319180001宁B63222

 

3、去除无用的头部和尾部xml,只保留Transaction标签中的内容

sed 's/.*<Transaction>//g;s/<\/OBU>.*<\/Message>//g' 2 > 3

 

结果如下

1110.001曹庄|宿州1|04|3401|804|33|20170321204036|03|3401|1105|1|201703211820562264011638220100098530宁B632221042157.602047.609439DAD2090134000030BC0018002DC4FE00000002000319180001宁B63222

 

4、将闭合标签替换为|||

sed 's/<\/[^>]*>/|||/g' 3 > 4

  

结果如下

1|||

 

5、将开始标签<***>去除

sed 's/<[^>]*>//g' 4 > 5

 

结果如下

1|||2017-03-21T20:40:36|||110.00|||1|||曹庄|宿州|||1|04|3401|804|33|20170321204036|03|3401|1105|1|20170321182056||||||22|||6401|||1638220100098530|||宁B63222|||104|||2157.60|||2047.60||||||9439DAD2|||09|||0134000030BC|||0018002D||||||C4FE|||0000000200031918|||0001|||宁B63222|||

 

到此大功告成

 

将所有标签整理在一起

cat ***.xml | tr "\n" " " > 1
sed 's/ //g;s/.*<Transaction>//g;s/<\/OBU>.*<\/Message>//g;s/<\/[^>]*>/|||/g;s/<[^>]*>//g' 1 > 2

 

转载于:https://www.cnblogs.com/miaocunf/p/6781541.html

你可能感兴趣的:(利用sed将xml报文转换为分隔符形式报文)