Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl如何在用于查找文件的变量中设置通配符(如果只知道部分文件名)_Perl_Variables_Wildcard - Fatal编程技术网

Perl如何在用于查找文件的变量中设置通配符(如果只知道部分文件名)

Perl如何在用于查找文件的变量中设置通配符(如果只知道部分文件名),perl,variables,wildcard,Perl,Variables,Wildcard,下面是代码,我读取一个到达ARGV[0]的文件,每次读取一行,并使用该行作为文件的部分名称,该文件的位置未知,因此使用find查找文件的位置(如果存在),然后将第二个参数ARGV[1]应用于该文件的grep if ($#ARGV != 1) { print "Usage : splfind.pl fullpath_of_input_file_contains_partial_filesnames_to_search pattern_to_search_on_filename_matche

下面是代码,我读取一个到达ARGV[0]的文件,每次读取一行,并使用该行作为文件的部分名称,该文件的位置未知,因此使用find查找文件的位置(如果存在),然后将第二个参数ARGV[1]应用于该文件的grep

if ($#ARGV != 1)
{
    print "Usage : splfind.pl fullpath_of_input_file_contains_partial_filesnames_to_search pattern_to_search_on_filename_matched_file";
    exit 1;
}

my $partfilelist_file=$ARGV[0];
my $stringtosearch=$ARGV[1];

my @partfilelist = split /\n/, `cat $partfilelist_file`;
while (defined ($partfileentry = pop(@partfilelist)))
{
    print "===============================================================================\n";    
    #print "Searching for file with partial name $partfileentry as [find . -type f -name '*$partfileentry*.c']\n";
    my $foundfile = `find . -type f -name '*$partfileentry*.c'`;
    if ($foundfile)
    {
        print "Found file $foundfile\n";
        system ("grep $stringtosearch $foundfile");
    }
    else
    {
        print "No file is found...\n";
    }
}
我现在得到的输出

[rajeguna@ukstbuild3 suites]$ splfind.pl ~/ipclient-test.txt SEARCH_
===============================================================================
*.c']find . -type f -name '*DMS_1319_4MS_1319_4
No file is found...
===============================================================================
*.c']find . -type f -name '*DMS_1319_3MS_1319_3
No file is found...
===============================================================================
*.c']find . -type f -name '*DMS_1288_1MS_1288_1
No file is found...
===============================================================================
*.c']find . -type f -name '*DMS_1283_1MS_1283_1
No file is found...
===============================================================================
*.c']find . -type f -name '*DMS_1282_2MS_1282_2
No file is found...
===============================================================================
*.c']find . -type f -name '*DMS_1282_1MS_1282_1
No file is found...
===============================================================================
我的输入文件包含以下内容

DMS_0307_6
DMS_0307_7
DMS_0392_1
DMS_0393_1
DMS_0397_10
DMS_0397_6
DMS_0397_7
DMS_0397_8
DMS_0397_9
DMS_0549_20
DMS_0549_22

我认为您不想使用glob操作符
。只需尝试在
find
命令中直接指定wilcard:

my @files = `find . -type f -name *DMS_0307_*.c`;
更新:使用变量(您可能需要对shell使用单引号):


使用
File::Find
,这样可以避免文件系统中的争用条件等。匹配所需
子文件中的文件名

sub wanted {
  return unless /\.c\z/ and index($_, $partfileentry) >= 0;
  # some manipulation here, with $_ as the filename,
  # while chdir'd to the correct directory
}

但是这个(DMS_0307_307;)将出现在一个变量上,当我将它与变量一起使用时,它不会在我的$foundfile=
find中起作用-类型f-名称*$partfileentry*.c
;谢谢@toolic,但它仍然不适合我。请参阅我的更新块,在那里我添加了实际的代码。这个问题的措辞可以更清楚。
sub wanted {
  return unless /\.c\z/ and index($_, $partfileentry) >= 0;
  # some manipulation here, with $_ as the filename,
  # while chdir'd to the correct directory
}