按模式过滤文件名

Bi.*_*Bi. 7 perl grep readdir

我需要搜索以特定模式开头的目录中的文件,比如说"abc".我还需要消除结果中以".xh"结尾的所有文件.我不知道如何在Perl中做到这一点.

我有这样的事情:

opendir(MYDIR, $newpath);
my @files = grep(/abc\*.*/,readdir(MYDIR)); # DOES NOT WORK
Run Code Online (Sandbox Code Playgroud)

我还需要从结果中删除所有以".xh"结尾的文件

谢谢,毕

Ale*_*own 7

尝试

@files = grep {!/\.xh$/} <$MYDIR/abc*>;
Run Code Online (Sandbox Code Playgroud)

其中MYDIR是一个包含目录路径的字符串.


Sin*_*nür 7

opendir(MYDIR,$ newpath); 我的@files = grep(/ abc*.*/,readdir(MYDIR)); #DOES不工作

你正在混淆一个带有glob模式的正则表达式模式.

#!/usr/bin/perl

use strict;
use warnings;

opendir my $dir_h, '.'
    or die "Cannot open directory: $!";

my @files = grep { /abc/ and not /\.xh$/ } readdir $dir_h;

closedir $dir_h;

print "$_\n" for @files;
Run Code Online (Sandbox Code Playgroud)