如何用perl刪除檔案內指定區域的字串?

如何用perl刪除檔案內指定區域的字串?

各位高手
如何用perl刪除檔案內指定區域的字串
例如刪除frame_start到frame_end之間的所有字串(紅字區域)

FLASH, 37, 315291, 77443, positive
FLASH, 41, 315293, 77443, negative
FLASH, 41, 315293, 56643, negative
FLASH, 42, 315293, 77443, positive
FLASH, 42, 315293, 56643, positive
LINE, 83, 315291, 59843, 315291, 74243, negative
FLASH, 84, 315291, 63683, positive
FLASH, 84, 315291, 65123, positive
FLASH, 84, 315291, 66563, positive
FLASH, 84, 315291, 68003, positive
FLASH, 84, 315291, 69443, positive
FLASH, 86, 111810, 362231, negative
step_end
sr_start panel
  16000,17600,0,NM,DELTA= 0:0, REPEAT = 1:1
sr_end
frame_start
FLASH, 88, 19994, 13594, negative
FLASH, 88, 331910, 387882, negative
FLASH, 88, 331910, 13594, negative
FLASH, 90, 159952, 13594, positive
FLASH, 90, 159952, 389482, positive
LINE, 63, 1600, 400000, 339200, 400000, negative
LINE, 63, 1600, 400000, 9600, 400000, positive
LINE, 63, 17600, 400000, 25600, 400000, positive
LINE, 63, 33600, 400000, 41600, 400000, positive
LINE, 63, 49600, 400000, 57600, 400000, positive
LINE, 63, 65600, 400000, 73600, 400000, positive
LINE, 63, 81600, 400000, 89600, 400000, positive
LINE, 63, 99200, 400000, 107200, 400000, positive
LINE, 63, 115200, 400000, 123200, 400000, positive
frame_end
FLASH, 81, 318110, 382564, positive
FLASH, 82, 316220, 383225, positive
LINE, 80, 320000, 22872, 305071, 22872, positive
LINE, 80, 305071, 22872, 305071, 18400, positive
LINE, 80, 305071, 18400, 320000, 18400, positive
LINE, 80, 320000, 18400, 320000, 22872, positive
CONTOUR, negative, HOLES_NUM = 0
    FROM, 320000, 22872
    LINETO, 320000, 18400
    LINETO, 305071, 18400
    LINETO, 305071, 22872
    LINETO, 320000, 22872
CONTOUR_END
这里可以用 range 操作符 ..


[Copy to clipboard] [ - ]
CODE:
use strict;
use warnings;

my($ok)=(1);
open FD,"<data.txt";
while(<FD>){
    if(/^frame_start$/){
        $ok=0;
    }
    if($ok){print;}
    if(/^frame_end$/){
        $ok=1;
    }
}



[Copy to clipboard] [ - ]
CODE:
    if(/^frame_start$/){
        $ok=0;
    }
    if($ok){print;}
    if(/^frame_end$/){
        $ok=1;
    }

等效于:

[Copy to clipboard] [ - ]
CODE:
if( not /^frame_start$/../^frame_end$/ ){
    print;
}

学习了~

但是我还不明白,这里的/^frame_start$/../^frame_end$/表示的是文本句柄的一个范围,还是perl根据匹配的布尔值来判断的啊?


QUOTE:
原帖由 __lxmxn__ 于 2007-12-11 15:34 发表
学习了~

但是我还不明白,这里的/^frame_start$/../^frame_end$/表示的是文本句柄的一个范围,还是perl根据匹配的布尔值来判断的啊?

perldoc perlop
查 range 操作符的用法……
多谢兄的指引,已经找到啦~
謝謝各位高手的幫忙
終於解決這個問題了!
Thanks lot !