[請益] 如何在文件中多次跨行比對

看板Perl作者 (chris)時間10年前 (2015/02/06 21:06), 編輯推噓1(104)
留言5則, 4人參與, 最新討論串1/1
請教各位先進 如何對文件進行多次的跨行比對? 我試了幾種方法, 但結果仍然有問題; 如果在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://www.ptt.cc/bbs/Perl/M.1423227978.A.50B.html

02/06 22:49, , 1F
檔案開一次不夠 你可以開兩次阿?
02/06 22:49, 1F

02/06 23:08, , 2F
在while裡開嗎?
02/06 23:08, 2F

02/07 00:31, , 3F
檔案不大的話 就逐行存到陣列 之後你愛怎麼用怎麼用
02/07 00:31, 3F

02/07 10:28, , 4F
已解決, 感謝各位先進的提示
02/07 10:28, 4F

02/07 14:32, , 5F
//g
02/07 14:32, 5F
文章代碼(AID): #1KrBnAKB (Perl)
文章代碼(AID): #1KrBnAKB (Perl)