Perl脚本跳过一行

时间:2015-06-26 18:08:00

标签: regex perl

我有YAML文件作为输入。在输入文件中,我有销售和销售($)行。我只需要输出一行作为输出 逻辑:

  1. 如果存在sales($)行,那么我们需要占用此行。
  2. 如果销售($)和销售行都存在,那么我们需要销售($)行。
  3. 如果销售行存在,那么我们需要销售行。(仅当销售行存在时)
  4. 任何人都可以指导如何实现这个perl 5吗?

    我的输入文件看起来像......

    ---
    Order ID: W00875205405667
    Sales($): '7.5'
    Transaction Datetime: 2015-03-29 16:17:00
    ---
    Order ID: W00875205405668
    Sales: '74.50'
    Sales($): '72.5'
    Transaction Datetime: 2015-03-29 16:17:00
    ---
    Order ID: W00875205405669
    Sales: '12.50'
    Transaction Datetime: 2015-03-29 16:17:00
    ---
    

    需要输出文件:

    ---
    Order ID: W00875205405667
    Sales: '7.5'
    Transaction Datetime: 2015-03-29 16:17:00
    ---
    Order ID: W00875205405668
    Sales: '72.50'
    Transaction Datetime: 2015-03-29 16:17:00
    ---
    Order ID: W00875205405669
    Sales: '12.50'
    Transaction Datetime: 2015-03-29 16:17:00
    ---
    

1 个答案:

答案 0 :(得分:1)

您可以优先选择销售($)只使用一个捕获缓冲区 使用分支重置构造(?|,,)

 # /(?m)^Order\h+ID:\h*(.*)\s+(?|(?:^.*\s+)?^Sales\(\$\):\h*(.*)\s+|^Sales:\h*(.*)\s+)^Transaction\h+Datetime:\h*(.*)/

 (?m)
 ^ Order \h+ ID: \h* 
 ( .* )                        # (1)
 \s+ 
 (?|
      (?: ^ .* \s+ )?
      ^ Sales\(\$\): \h* 
      ( .* )                        # (2)
      \s+ 
   |  
      ^ Sales: \h* 
      ( .* )                        # (2)
      \s+ 
 )
 ^ Transaction \h+ Datetime: \h* 
 ( .* )                        # (3)

输出:

 **  Grp 0 -  ( pos 0 , len 85 ) 
Order ID: W00875205405667
Sales($): '7.5'
Transaction Datetime: 2015-03-29 16:17:00  
 **  Grp 1 -  ( pos 10 , len 15 ) 
W00875205405667  
 **  Grp 2 -  ( pos 37 , len 5 ) 
'7.5'  
 **  Grp 3 -  ( pos 66 , len 19 ) 
2015-03-29 16:17:00  

-------------------

 **  Grp 0 -  ( pos 92 , len 102 ) 
Order ID: W00875205405668
Sales: '74.50'
Sales($): '72.5'
Transaction Datetime: 2015-03-29 16:17:00  
 **  Grp 1 -  ( pos 102 , len 15 ) 
W00875205405668  
 **  Grp 2 -  ( pos 145 , len 6 ) 
'72.5'  
 **  Grp 3 -  ( pos 175 , len 19 ) 
2015-03-29 16:17:00  

-------------------

 **  Grp 0 -  ( pos 201 , len 84 ) 
Order ID: W00875205405669
Sales: '12.50'
Transaction Datetime: 2015-03-29 16:17:00  
 **  Grp 1 -  ( pos 211 , len 15 ) 
W00875205405669  
 **  Grp 2 -  ( pos 235 , len 7 ) 
'12.50'  
 **  Grp 3 -  ( pos 266 , len 19 ) 
2015-03-29 16:17:00