あなたが説明したことを行うのに役立ついくつかのPerlツールを次に示します。
- File::Findは、ディレクトリとその子のファイルを再帰的に検索し、
\&wanted
各ファイルに対してコード (ドキュメントのコールバック) を実行して、基準を満たすかどうかを判断します。
-r
オペレータは、ファイルが読み取り可能かどうかを教えてくれます( if (-r $file_name)...
)
- openはファイルへのアクセスを取得し、
<$fh>
その内容を読み取って、ターゲットパターンと一致するかどうかを正規表現で確認できるようにします
\b
パターンの先頭と末尾に追加すると、grep
の-w
switchと同様に、単語の境界でのみ一致するようになります
より具体的な問題がある場合は、発生すると予想したことと、実際の結果が予想とどのように異なっていたかの両方のステートメントを含め、それらを示すコードとともに追加の質問を投稿してください。これらの問題の解決を喜んでお手伝いします.
編集:コメントからクリーンアップされ、実行可能なバージョンのコード:
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;
use File::Find;
# Get $dirname from first command-line argument
my $dirname = shift @ARGV;
find(\&do_process, $dirname); # quotes around $dirname weren't needed
my ($KeyMnode, $KeyThreads);
sub do_process {
# chomp($_); - not needed; $_ isn't read from a file, so no newline on it
if (-r $_) { # quotes around $_ weren't needed
# $_ is just the final part of the file name; it may be better for
# reporting the location of matches to set $file_name to
# $File::Find::name instead
my $file_name = $_;
open(my $fh, '<', $file_name); # Use three-arg open!
while (<$fh>) {
chomp();
# Note that, if you store all matches into the same scalar values,
# you'll end up with only the last value found for each pattern; you
# may want to push the matches onto arrays instead.
if (/\bkeyword : Multinode\b/i) { $KeyMnode = "$file_name:$_"; }
if (/\bkeyword : Threads\b/i) { $KeyThreads = "$file_name:$_"; }
}
}
}