続きです。
今回は–authorオプションの機能を追加します。
では、変更後のソースです。
takk@deb9:~$ cat -n t.pl
1 $_ = "@ARGV";
2 s/--(\w+-?\w+) /--$1=/g;
3 $author='.*';
4 $text='.*';
5
3,4行目が追加箇所です。
前回追加したtextオプションと、今回のauthorオプションに関して、デフォルトで全部抽出できるように、.*にしています。
6 foreach(split / /){
7 $author=$1 if(/--author=(\S+)/);
8 $text=$1 if(/--text=(\S+)/);
9 $exclude=$1 if(/--exclude=(\S+)/);
10 $from_date=$1 if(/--from-date=(\S+)/);
11 $to_date=$1 if(/--to-date=(\S+)/);
12 }
13
14 %log=();
15 opendir(DIR,".");
16 @files = grep { /ChangeLog.*/ } readdir(DIR);
17 closedir(DIR);
18
19 $num = 1;
20 foreach(@files){
21 open(INFILE,"<$_");
22 $id = "";
23 while(<INFILE>){
24 chomp();
25 s/^ /\t/;
26 if(/^\d\d\d\d\-\d\d\-\d\d/){
27 $title = "$_";
28 $num=0;
29 $id = $title .':::'. $num++;
30 next;
31 }
32 if(/^ \*/){
33 $id = $title .':::'. $num++;
34 }
6~34行目、変更なしです。
35 if(/^ |^ \*|^$/ && $id ne ""){
36 if(/$text/){
37 $log{$id} .= "$_";
38 $found{$id} ++;
39 }
40 next;
41 }
42 last;
前回のスクリプトでは、$found{$id} ++ if(/$text/);としていたので、指定した文字列のみが表示されていました。オプションなしの場合に何も表示されなくなるので、37,38行目は、if(/$test/)で囲みます。
43 }
44
45 close(INFILE);
46 }
47
48 foreach(sort keys %log){
49 $id = $_;
50 @tmp = split /\t/,$log{$_};
51
43~51行目、変更なしです。
52 if($found{$id} > 0){
53 if($id =~ /$author/){
54 print "$id\n";
55 foreach(@tmp){
56 print" $_\n";
57 }
58 print"\n";
59 }
60 }
61 }
takk@deb9:~$
53,59行目に、authorオプションが指定された場合に、判定文を追加しました。これで指定したauthorのログのみ抽出できるようになります。


コメント