作者dxf91qqq (chris)
看板Perl
標題[請益] 如何在文件中多次跨行比對
時間Fri Feb 6 21:06:16 2015
請教各位先進
如何對文件進行多次的跨行比對?
我試了幾種方法,
但結果仍然有問題;
如果在while前用$/="";的方式會變成整個文件只比對一次;
但一旦用while讀文件內容就只能一次讀一行,
變成比對不到想要的模式;
程式碼如下:
#! \strawberry\perl\bin\perl
use warnings;
use strict;
use 5.010;
use autodie;
open FILE, "<123.txt";
while (<FILE>) #也可不寫檔案代碼
{
if(/test_result: 1,2,3,4.*?test_name: iddq/sm)
{
print "match\n";
print $&;
}
}
close FILE;
想比對的文件123.txt內容如下:
dfklqwejpfwejkpofwkjp
test_result: 1,2,3,4
test_name: iddq
test_unit: uA
kdjkfjwojoew
gvaksljgwrl
hbrewhg
test_result: 12,22,33,44
test_name: iddq1
test_unit: uA
fgewgerjwogwrj
gwejqoej
test_result: 13,2,3,4
test_name: iddq
test_unit: uA
oljoifjwqeoijfweo
jfowejqoifjfeoi
test_result: 14,2,3,4
test_name: iddq
test_unit: uA
blahblahblahblah
[3.jpg]
欄位1 欄位2 欄位3 欄位4 資料1 資料2 資料3 資料4
[4.jpg]
blahblahblahblah
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.169.240.149
※ 文章網址: https://webptt.com/m.aspx?n=bbs/Perl/M.1423227978.A.50B.html
1F:推 abliou: 檔案開一次不夠 你可以開兩次阿? 02/06 22:49
2F:→ dxf91qqq: 在while裡開嗎? 02/06 23:08
3F:→ CCWck: 檔案不大的話 就逐行存到陣列 之後你愛怎麼用怎麼用 02/07 00:31
4F:→ dxf91qqq: 已解決, 感謝各位先進的提示 02/07 10:28
5F:→ herculus6502: //g 02/07 14:32