按模式过滤文件名

发布于 2024-07-23 23:01:12 字数 260 浏览 8 评论 0原文

我需要在目录中搜索以特定模式开头的文件,例如“abc”。 我还需要删除结果中以“.xh”结尾的所有文件。 我不知道如何用 Perl 来做这件事。

我有这样的事情:

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

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

谢谢,Bi

I need to search for files in a directory that begin with a particular pattern, say "abc". I also need to eliminate all the files in the result that end with ".xh". I am not sure how to go about doing it in Perl.

I have something like this:

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

I also need to eliminate all files from result that end with ".xh"

Thanks, Bi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦里梦着梦中梦 2024-07-30 23:01:12

尝试

@files = grep {!/\.xh$/} <$MYDIR/abc*>;

其中 MYDIR 是包含目录路径的字符串。

try

@files = grep {!/\.xh$/} <$MYDIR/abc*>;

where MYDIR is a string containing the path of your directory.

一袭白衣梦中忆 2024-07-30 23:01:12

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

您将正则表达式模式与全局模式混淆了。

#!/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;

opendir(MYDIR, $newpath); my @files = grep(/abc*.*/,readdir(MYDIR)); #DOES NOT WORK

You are confusing a regex pattern with a glob pattern.

#!/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;
唐婉 2024-07-30 23:01:12
opendir(MYDIR, $newpath) or die "$!";
my @files = grep{ !/\.xh$/ && /abc/ } readdir(MYDIR);
close MYDIR;
foreach (@files) { 
   do something
}
opendir(MYDIR, $newpath) or die "$!";
my @files = grep{ !/\.xh$/ && /abc/ } readdir(MYDIR);
close MYDIR;
foreach (@files) { 
   do something
}
╭⌒浅淡时光〆 2024-07-30 23:01:12

kevinadc 和 Sinan Unur 使用但没有提及的一点是,当在列表上下文中调用时,readdir() 会返回目录中所有条目的列表。 然后您可以使用任何列表运算符。 这就是为什么您可以使用:

my @files = grep (/abc/ && !/\.xh$/), readdir MYDIR;

So:

readdir MYDIR

返回 MYDIR 中所有文件的列表。

并且:

grep (/abc/ && !/\.xh$/)

返回由 readdir MYDIR 返回的所有符合条件的元素。

The point that kevinadc and Sinan Unur are using but not mentioning is that readdir() returns a list of all the entries in the directory when called in list context. You can then use any list operator on that. That's why you can use:

my @files = grep (/abc/ && !/\.xh$/), readdir MYDIR;

So:

readdir MYDIR

returns a list of all the files in MYDIR.

And:

grep (/abc/ && !/\.xh$/)

returns all the elements returned by readdir MYDIR that match the criteria there.

因为看清所以看轻 2024-07-30 23:01:12
foreach $file (@files)
{
    my $fileN = $1 if $file =~ /([^\/]+)$/;

    if ($fileN =~ /\.xh$/)
    {
          unlink $file;
          next;
    }
    if ($fileN =~ /^abc/)
    {
          open(FILE, "<$file");
          while(<FILE>)
          {
             # read through file.
          }
    }
 }

另外,可以通过执行以下操作来访问目录中的所有文件:

$DIR = "/somedir/somepath";
foreach $file (<$DIR/*>)
{
  # apply file checks here like above.
}

或者,您可以使用 perl 模块 File::find。

foreach $file (@files)
{
    my $fileN = $1 if $file =~ /([^\/]+)$/;

    if ($fileN =~ /\.xh$/)
    {
          unlink $file;
          next;
    }
    if ($fileN =~ /^abc/)
    {
          open(FILE, "<$file");
          while(<FILE>)
          {
             # read through file.
          }
    }
 }

also, all files in a directory can be accessed by doing:

$DIR = "/somedir/somepath";
foreach $file (<$DIR/*>)
{
  # apply file checks here like above.
}

ALternatively you can use the perl module File::find.

女中豪杰 2024-07-30 23:01:12

您可以使用 glob,而不是使用 opendir 和过滤 readdir(不要忘记 linedir!) :

use File::Spec::Functions qw(catfile splitpath);

my @files =
    grep !/^\.xh$/,                # filter out names ending in ".xh"
    map +(splitpath $_)[-1],       # filename only
    glob                           # perform shell-like glob expansion
        catfile $newpath, 'abc*';  # "$newpath/abc*" (or \ or :, depending on OS)

如果您不关心消除 glob,去掉 map+分割路径

Instead of using opendir and filtering readdir (don't forget to closedir!), you could instead use glob:

use File::Spec::Functions qw(catfile splitpath);

my @files =
    grep !/^\.xh$/,                # filter out names ending in ".xh"
    map +(splitpath $_)[-1],       # filename only
    glob                           # perform shell-like glob expansion
        catfile $newpath, 'abc*';  # "$newpath/abc*" (or \ or :, depending on OS)

If you don't care about eliminating the $newpath prefixed to the results of glob, get rid of the map+splitpath.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文