-2

这是我读取到达 ARGV[0] 的文件并一次读取一行并将该行用作文件的部分名称的代码,该文件的位置未知,因此使用查找来查找如果存在文件,则将第二个参数 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
4

2 回答 2

0

使用File::Find,这将使您避免文件系统中的竞争条件等。匹配您的sub wanted.

sub wanted {
  return unless /\.c\z/ and index($_, $partfileentry) >= 0;
  # some manipulation here, with $_ as the filename,
  # while chdir'd to the correct directory
}
于 2013-10-09T18:31:54.587 回答
0

我认为您不想使用 glob 运算符<>。只需尝试直接在find命令中指定通配符:

my @files = `find . -type f -name *DMS_0307_*.c`;

更新:使用变量(您可能需要在 shell 中使用单引号):

my $partfileentry = 'DMS_1189_';
my @files = `find . -type f -name '*$partfileentry*.c'`;
于 2013-10-09T17:34:14.160 回答