2015-02-10 86 views
0

我是Perl的新手。我必须完成一项任务。我有一个目录,并在该目录中有130个子目录。从这些目录有报告子目录和报告,有一个.txt文件,我想从该文件中获取模式。Perl:遍历目录并从文件中获取模式

同样,我必须做所有的事情,并得到模式。

任何人都可以请示例代码帮助或如何做到这一点?

任何帮助将不胜感激。

+7

欢迎来到Stack Overflow!你能告诉我们你到目前为止所尝试过的吗? – 2015-02-10 02:36:18

+3

你不能只问别人为你做你的全部工作。试试自己的东西,告诉我们你做了什么。现在,您可以向我们询问您遇到麻烦的某个地区。 – 2015-02-10 05:04:49

+4

查看[File :: Find](http://perldoc.perl.org/File/Find.html)模块。它涵盖了你想要完成的一大部分。 – 2015-02-10 05:12:42

回答

2

开始与此:

#!/bin/env perl 
use strict; use warnings; 

my ($dir) = @ARGV; 
$dir // die "No dir supplied"; 

die "Dir not found: $dir" unless (-e $dir); 
die "Not a dir: $dir" unless (-d $dir); 

my @files = <$dir/*/txtfile.txt>; 

foreach my $file (@$files) { 
    my $file_contents = undef; 

    # read the patten from file.. 
    open (my $fh, '<', $file) or die "Can't open $file. $!"; 
    # Read the file contents here.. 
    { 
     local $/ = undef; 
     $file_contents = <$fh>; 
    } 
    close $fh; 

    # Do something more 
} 

这种方法被称为文件通配符。使用专门编写的模块有更好的方法,但这种方法很有效。这与在终端窗口输入ls -l mydir/*/txtfile.txt是一样的。