• 追加された行はこの色です。
  • 削除された行はこの色です。
* File::Grep [#w1f51751]

** サンプルのテキストファイル [#f193118d]
*** a.txt [#d43001f3]
 a
 b
 c x
 a
*** b.txt [#t129e8f9]
 a
 b
*** c.txt [#e112c6a6]
 b

** fgrep [#df4226ee]
 
 use File::Grep qw(fgrep);
 use Data::Dumper;
 
 @ret = fgrep {/a/} glob './*.txt';
 print Dumper @ret;

 $VAR1 = {
     'count' => 2,
     'matches' => {
        '4' => 'a
 ',
        '1' => 'a
 '
         },
     'filename' => './a.txt'
   };
 $VAR2 = {
     'count' => 1,
     'matches' => {
        '1' => 'a
 '
         },
     'filename' => './b.txt'
   };
 $VAR3 = {
     'count' => 0,
     'matches' => {},
     'filename' => './c.txt'
   };

fgrep()をスカラーで受けると($ret=fgrep {...} ...)、真偽値(0か1)が入る。

** fgrep {($file,$pos,$line)=@_} [#fe015621]
条件式の中でファイルの通し番号や行番号を取り出せる。
 use File::Grep qw(fgrep);
 
 @ret = fgrep {($file,$pos,$line)=@_;print "$file,$pos,$line";} glob './*.txt';

 0,1,a
 0,2,b
 0,3,c x
 0,4,a
 1,1,a
 1,2,b

$fileはgrepしたファイルの、0から始まる通し番号をあらわす。

** 参考 [#x2039a5b]
http://search.cpan.org/~mneylon/File-Grep-0.02/Grep.pm



トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS