gpt4 book ai didi

perl - 如何使用 Perl 将输出写入新的外部文件?

转载 作者:行者123 更新时间:2023-12-05 00:06:46 25 4
gpt4 key购买 nike

也许我使用错误的关键字进行搜索,或者这是一个非常基本的问题,但我找不到我的问题的答案。我在将 whois 命令的结果写入新的外部文件时遇到问题。

我的代码如下。需要 $readfilename ,这是一个包含 IP 列表的文件名,以及 $writefilename ,这是输出的目标文件。两者都是用户指定的。对于我的测试,$readfilename在三个单独的行中包含三个 IP 地址,因此在用户指定的输出文件中应该有三个单独的 whois 结果。

if ($readfilename) {
open (my $inputfile, "<", $readfilename) || die "\n Cannot open the specified file. Please double check your file name and path.\n\n";
open (my $outputfile, ">", $writefilename) || die "\n Could not create write file.\n\n";
while (<$inputfile>) {
my $iplookupresult = `whois $_ > $writefilename`;
print $outputfile $iplookupresult;
}
close $outputfile;
close $inputfile;
}

我可以执行此脚本并最终得到一个新的外部文件,但该文件的一半以上具有二进制垃圾数据(在 CentOS 上运行),并且只有一个(或其中的一部分)whois 查询是可读的。

我不知道我的文件有一半是如何变成二进制的……但我的方法肯定是不正确的。有没有更好的方法来达到同样的结果?

最佳答案

您正在使用 shell 重定向来重定向 whois 的输出到一个文件。但是您也打开了该文件进行写入并尝试将数据写入同一个文件,这给您带来了垃圾。只需删除 shell 重定向:

print $outputfile `whois $_`;

关于perl - 如何使用 Perl 将输出写入新的外部文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2870887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com