作者giacch (小a)
看板Perl
标题Re: [问题] 多个关键字的搜寻
时间Sun Sep 14 01:30:27 2008
※ 引述《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