Re: [問題] 多個關鍵字的搜尋
※ 引述《trytrytry (try)》之銘言:
: 想用多個連續關鍵字來搜尋某一目錄下的所有檔案
: 例如有一個檔案內容如下
: structure
: union
: map
: ...
: character aaa
: byte bbb
: ...
: integer ccc
: ...
: end structure
: end union
: end map
: 這個程式主要用structure union map搜尋,符合這字串後面的結構中若也帶有integer
: 則輸出此檔案名字
: 已爬文,但似乎找不到regression expression用多個關鍵字來搜尋
: 可否請版上的強者幫忙回答一下
: 謝謝!
是不是這個意思..?
#!/usr/bin/perl
$DIR = '/tmp';
opendir(DIR, $DIR) or die "$DIR: $!\n";
@Files = grep { -f } map { "$DIR/$_" } readdir(DIR);
close(DIR);
for $File (@Files) {
open(TMP, $File) or die "$File: $!\n";
while(<TMP>) {
for $Key ("structure", "union", "map") {
$Find{$Key} = 1 if(/^$Key$/);
if(/integer/ and $Find{$Key}) { print $File . "\n"; last; }
undef $Find{$Key} if(/^end $Key$/);
}
}
close(TMP);
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.232.236.185
討論串 (同標題文章)