我有以下代码,我希望输出到.txt文件,所以有人请帮助打印输出到某个文件? 相反它应该有一个用户选择推送到文件或打印到命令提示符本身。
# Opening Keyword File here
open( my $kw, '<', 'IMSRegistration_Success_MessageFlow.txt') or die $!;
my @keywords = <$kw>;
chomp(@keywords); # remove newlines at the end of keywords
# post-processing your keywords file for adding comments
my $kwhashref = {
map {
/^(.*?)(#.*?#)*$/;
defined($2) ? ($1 => $2) : ( $1 => undef )
} @keywords
};
# get list of files in current directory
my @files = grep { -f } (<*main_log>,<*Project>,<*properties>);
# loop over each file to search keywords in
foreach my $file (@files)
{
open(my $fh, '<', $file) or die $!;
my @content = <$fh>;
close($fh);
my $l = 0;
foreach my $kw (keys %$kwhashref)
{
my $search = quotemeta($kw); # otherwise keyword is used as regex, not literally
foreach (@content)
{ # go through every line for this keyword
$l++;
if (/$search/)
{
print $kwhashref->{$kw}."\n" if defined($kwhashref->{$kw}) ;
printf 'Found keyword %s in file %s, line %d:%s'.$/, $kw, $file, $l, $_
}
}
}
}
答案 0 :(得分:0)
script.pl >output.txt
答案 1 :(得分:0)
我可以使用以下代码将输出转换为文件:
print $ out_file $ kwhashref-&gt; {$ kw}。&#34; \ n&#34;如果定义($ kwhashref-&gt; {$ kw}); printf $ out_file&#39;在文件%s中找到关键字%s,行%d:%s&#39;。$ /,$ kw,$ file,$ l,$ _;