ファイルに式が出現した回数をカウントする Perl スクリプトがあります。この特定のケースでは、.xml ファイルを解析したかったので、'<' と '>' の間にあるすべてをカウントします。
脚本:
#usr/bin/perl
sub by_count {
$count{$b} <=> $count{$a};
}
open(INPUT, "<[Content_Types].xml");
open(OUTPUT, ">output");
$bucket = qw/./;
while(<INPUT>){
@words = split(/\</);
foreach $word (@words){
if($word=~/($bucket*>)/io){
#print OUTPUT "$word";
#print OUTPUT "\n\n";
$count{$1}++;}
}
}
foreach $word (sort by_count keys %count) {
print OUTPUT "<$word occurs $count{$word} times\n\n";
}
close INPUT;
close OUTPUT;
出力
<Default Extension="xlsx" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"/> occurs 1 times
<Default Extension="png" ContentType="image/png"/> occurs 1 times
<Override PartName="/word/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml"/> occurs 1 times
問題
再帰的にやりたい。内部に複数のサブディレクトリを含むディレクトリがあり、各サブフォルダー内に [Content_Types].xml ファイルがあります。メインディレクトリにあるその名前のすべてのファイルを解析する方法について何か提案はありますか?
例図:
>Directory
>Directory1
>[Content_Types].xml
>Directory2
>[Content_Types].xml
>Directory3
>[Content_Types].xml
.
.
.
>Directory100
>[Content_Types].xml