如何使用扩展名进行grep文件名检查并使用Perl存储在数组中



>我们在目录中有一些PDF文件

abc.pdf20200623
chg.pdf20200624
guih.pdf20200614

我想 grep 所有这些.pdf文件并将其存储在一个数组中。下面是示例代码。

下面的代码无法从目录中查找pdf文件,我认为grep不起作用,这个问题的可能解决方案是什么。

#!/usr/bin/perl -w
use strict
my sent_dir = "/abc/sfj/gjj";
opendir(DIR, "sent_dir");
my @file_local = grep(/.pdf$/i,readdir(DIR));
closedir(DIR) 

这就是glob()的用途。

use strict;
use warnings;
my $sent_dir = '/abc/sfj/gjj';
my @files = glob "$sent_dir/*pdf*";

在您的输入文件中没有extension *.pdf.

use strict;
use warnings;
my $dir = "C:\test";
opendir(DIR, "$dir") || die "Couldn't find the dir $dir: $!n";
my @allpdffiles = grep /.pdf([d-]+)?$/i, readdir(DIR);
closedir(DIR);
print join "n", @allpdffiles;
Output:
test1.pdf123  
test2.pdf456  
test3.pdf789

最新更新