今回は、前回のスクリプトを改造して、フィルタリングできるようにします。
前回のスクリプトはこれです。
takk@deb9:~/src/binutils-2.28/binutils$ cat -n t.pl
1 %log=();
2 opendir(DIR,".");
3 @files = grep { /ChangeLog/ } readdir(DIR);
4 closedir(DIR);
5
6 foreach(@files){
7 open(INFILE,"<$_");
8 $id = "";
9 while(<INFILE>){
10 chomp();
11 if(/^\d\d\d\d-\d\d-\d\d/){
12 $id = "$_";
13 next;
14 }
15 if(/^ |^$/ && $id ne ""){
16 $log{$id} .= "$_";
17 next;
18 }
19 last;
20 }
21
22 close(INFILE);
23 }
24
25 foreach(keys %log){
26 $id = $_;
27 @tmp = split /\t/,$log{$_};
28
29 print "$id\n";
30 foreach(@tmp){
31 print" $_\n";
32 }
33 print"\n";
34 }
takk@deb9:~/src/binutils-2.28/binutils$
変更1箇所目は、ファイルの先頭で、フィルタリングするパターンをパラメータから取得。
pattern = $ARGV[0];
1 %log=();
2 opendir(DIR,".");
3 @files = grep { /ChangeLog/ } readdir(DIR);
4 closedir(DIR);
2箇所目は、パターンにマッチするログを見つけた時に、フラグで覚えておく処理。
15 if(/^ |^$/ && $id ne ""){
16 $log{$id} .= "$_";
$found{$id} ++ if(/$pattern/);
17 next;
18 }
3箇所4箇所目は、フラグをチェックして表示するかどうかの判定する処理。
25 foreach(keys %log){
26 $id = $_;
27 @tmp = split /\t/,$log{$_};
28
if($found{$id} > 0){
29 print "$id\n";
30 foreach(@tmp){
31 print" $_\n";
32 }
33 print"\n";
}
34 }
使ってみます。
takk@deb9:~/src/binutils-2.28/binutils$ perl t.pl bin2c | head -20 2009-08-28 H.J. Lu <hongjiu.lu@intel.com> * Makefile.am (sysinfo$(EXEEXT_FOR_BUILD)): Replace CFLAGS/LDFLAGS with CFLAGS_FOR_BUILD/LDFLAGS_FOR_BUILD. (syslex.o): Likewise. (sysinfo.o): Likewise. (bin2c$(EXEEXT_FOR_BUILD)): Likewise. * Makefile.in: Regenerated. 2009-08-22 Ralf Wildenhues <Ralf.Wildenhues@gmx.de> * Makefile.am (AM_CPPFLAGS): Renamed from ... (INCLUDES): ... this. (bin2c$(EXEEXT_FOR_BUILD): Adjust rule. (installcheck-local): Renamed from ... (installcheck): ... this. * Makefile.in: Regenerate. * Makefile.am (AUTOMAKE_OPTIONS): Remove cygnus, add no-dist and foreign. (MKDEP, CLEANFILES): Remove now-unneeded variables. takk@deb9:~/src/binutils-2.28/binutils$
日付が順番に並んでないのが気になりますね。25行目のkeysを、sortで並べ替えしておきます。
25 foreach(sort keys %log){
26 $id = $_;
27 @tmp = split /\t/,$log{$_};
28
if($found{$id} > 0){
29 print "$id\n";
30 foreach(@tmp){
31 print" $_\n";
32 }
33 print"\n";
}
34 }
takk@deb9:~/src/binutils-2.28/binutils$ perl tt.pl bin2c | sed -ne '/^2/p' 2008-08-25 Alan Modra <amodra@bigpond.net.au> 2009-08-22 Ralf Wildenhues <Ralf.Wildenhues@gmx.de> 2009-08-28 H.J. Lu <hongjiu.lu@intel.com> 2010-11-05 Alan Modra <amodra@gmail.com> takk@deb9:~/src/binutils-2.28/binutils$
grep-changelogの結果と比較してみます。
takk@deb9:~/src/binutils-2.28/binutils$ grep-changelog --text bin2c ChangeLog* | sed -ne '/^2/p' 2007-07-05 Nick Clifton <nickc@redhat.com> 2007-02-28 Alan Modra <amodra@bigpond.net.au> 2007-02-28 Alan Modra <amodra@bigpond.net.au> 2007-02-27 Alan Modra <amodra@bigpond.net.au> 2007-02-27 Alan Modra <amodra@bigpond.net.au> 2007-02-27 Alan Modra <amodra@bigpond.net.au> 2008-08-25 Alan Modra <amodra@bigpond.net.au> 2009-08-28 H.J. Lu <hongjiu.lu@intel.com> 2009-08-22 Ralf Wildenhues <Ralf.Wildenhues@gmx.de> 2010-11-05 Alan Modra <amodra@gmail.com> 2010-11-05 Alan Modra <amodra@gmail.com> 2010-01-26 Tristan Gingold <gingold@adacore.com> takk@deb9:~/src/binutils-2.28/binutils$
抽出されるログの数が異なりました。
次回スクリプトの間違いを探していきます。


コメント