2017-08-29 87 views
-1

我在我的Linux機器中有一個XML文件&我想讀取未被評論的行。 例子:如何使用sed/awk獲取XML uncommented部分

輸入文件

<?xml version="1.0" encoding="UTF-8"?> 

<!-- This is an example 
    don't read it while working --> 
<ccb> 
       <ccc> 
         <aaa>true</aaa> 
         <bbb>name_1</bbb> 
         <Port>1534</Port> 
      <datPort>1532</datPort> 
<!-- 
         <e214> 
           <ImsiPrefixLen>5</ImsiPrefixLen> 
           <LocalPrefix>97252</LocalPrefix> 
         </e214> 
--> 

       </ccc> 
</ccb> 

輸出文件:

<?xml version="1.0" encoding="UTF-8"?> 
<ccb> 
       <ccc> 
         <aaa>true</aaa> 
         <bbb>name_1</bbb> 
         <Port>1534</Port> 
      <datPort>1532</datPort> 
       </ccc> 
</ccb> 
+0

不要使用'sed'或'awk';使用真正的XML解析器。 – chepner

回答

3

注意,在XML註釋與<!--開始,以-->結束;它不能包含--

perl -pe 'BEGIN{undef$/}s/<!--.*?-->//gs' <<END 
<?xml version="1.0" encoding="UTF-8"?> 

<!-- This is an example 
don't read it while working --> 
<ccb> 
      <ccc> 
        <aaa>true</aaa> 
        <bbb>name_1</bbb> 
        <Port>1534</Port> 
     <datPort>1532</datPort> 
<!-- 
        <e214> 
          <ImsiPrefixLen>5</ImsiPrefixLen> 
          <LocalPrefix>97252</LocalPrefix> 
        </e214> 
--> 

      </ccc> 
</ccb> 
END 

說明

perl -h 

-p : assume loop like -n but print line also, like sed 

BEGIN block executed once at beginning to unset the input record separator ($/) because of multiline matching <!-- --> 

s/// : substitute function (/ can be replaced by any other character) 

<!--.*?--> : .* any string ? lazy modifier to get the shortest match 

s : modifier so that . matches also newline character 
+0

謝謝,它工作!你能解釋一下命令「perl -pe'BEGIN {undef $ /} s/ // gs'」 – user3895453