对不起,如果这很冗长,但我有一个部分工作的 perl 脚本。我有一个正则表达式,可以提取其中一个foo|bar
和给定字符串的前缀。但问题是我的字符串也是文件名,我也想打开并检索它的内容locale_col.dat.2010120813.png
(见下面的预期输出)。
输出现在如下所示:
Content:/home/myhome/col/.my_file_del.mail@locale.foo.org
Key1:foo:Key2:col
Content:/home/myhome/col/.my_file_del.dp1.bar.net
Key1:bar:Key2:col
Content:/home/myhome/jab/.my_file_del.mail@locale.foo.org
Key1:foo:Key2:jab
Content:/home/myhome/jab/.my_file_del.dp1.bar.net
Key1:bar:Key2:jab
我需要帮助来调整它,以便一次性读取字符串列表(来自 FileList.txt 的文件名),从文件名路径中提取特定值(使用正则表达式)并打开文件名以获取其内容。我希望这是有道理的,还是我正在考虑将其分解为 2 个 perl 脚本?感谢您的输入。
代码(在制品):
open FILE, "< /home/myname/FileList.txt";
while (<FILE>) {
my $line = $_;
chomp($line);
print "Content:$_"; #This is just printing the filenames.
#I want to get the contents of those file names instead. Stuck here.
if ($line =~ m/home\/myname\/(\w{3}).*[.](\w+)[.].*/){
print "Key1:$2:Key2:$1\n";
}
}
close FILE;
FileList.txt 的内容:
/home/myname/col/.my_file_del.mail@locale.foo.org
/home/myname/col/.my_file_del.dp1.bar.net
/home/myname/jab/.my_file_del.mail@locale.foo.org
/home/myname/jab/.my_file_del.dp1.bar.net
列出的文件之一的示例内容:(我需要帮助来提取)
$ cat .my_file_del.mail@locale.foo.org
locale_col.dat.2010120813.png
预期输出:
Content:locale_col.dat.2010120813.png
Key1:foo:Key2:col
...
..