Perl 使用其他文件的输入输出到文件
我正在尝试使用另一个文件的输入输出到一个文件。没有键盘输入 我知道我在正确的轨道上,我的语法只是有点偏离 基本上,我从文件“boot.log”中获取记录,使用模式匹配选择某些记录并将它们输出到名为“bootlog.out”的文件中。我还没有谈到模式匹配部分。这是我所拥有的Perl 使用其他文件的输入输出到文件,perl,input,output,shell,Perl,Input,Output,Shell,我正在尝试使用另一个文件的输入输出到一个文件。没有键盘输入 我知道我在正确的轨道上,我的语法只是有点偏离 基本上,我从文件“boot.log”中获取记录,使用模式匹配选择某些记录并将它们输出到名为“bootlog.out”的文件中。我还没有谈到模式匹配部分。这是我所拥有的 open (BOOTLOG, "boot.log") || die "Can't open file named boot.log: $!"; while ($_ = <BOOTLOG>) { print $_;
open (BOOTLOG, "boot.log") || die "Can't open file named boot.log: $!";
while ($_ = <BOOTLOG>)
{
print $_;
}
open (LOGOUT, ">bootlog.out") || die "Can't create file named bootlog.out: $!\n";
close (LOGOUT) || die "Can't close file named bootlog.out: $!\n";
close (BOOTLOG) || die "Can't close the file named boot.log: $!";
open(BOOTLOG,“boot.log”)| | die“无法打开名为boot.log:$!”的文件;
while($\=)
{
打印美元;
}
打开(注销“>bootlog.out”)| | die“无法创建名为bootlog.out:$!\n”的文件;
关闭(注销)| | die“无法关闭名为bootlog.out:$!\n”的文件;
close(BOOTLOG)| | die“无法关闭名为boot.log:$!”的文件;
如何将boot.log的内容打印到bootlog.out
编辑1
这似乎将输入和输出到第二个文件。语法正确吗
open (BOOTLOG, "boot.log") || die "Can't open file named boot.log: $!";
open (LOGOUT, ">bootlog.txt") || die "Can't create file named bootlog.out: $!\n";
while ($_ = <BOOTLOG>)
{
print $_;
print LOGOUT $_;
}
close (LOGOUT) || die "Can't close file named bootlog.txt: $!\n";
close (BOOTLOG) || die "Can't close the file named boot.log: $!";
open(BOOTLOG,“boot.log”)| | die“无法打开名为boot.log:$!”的文件;
打开(注销,“>bootlog.txt”)| | die“无法创建名为bootlog.out:$!\n”的文件;
while($\=)
{
打印美元;
打印注销$;
}
关闭(注销)| | die“无法关闭名为bootlog.txt的文件:$!\n”;
close(BOOTLOG)| | die“无法关闭名为boot.log:$!”的文件;
只需使用输出文件句柄注销
和打印
。在实际打印到输出文件句柄之前,还需要打开输出文件句柄
open (BOOTLOG, "boot.log") || die "Can't open file named boot.log: $!";
open (LOGOUT, ">bootlog.out") || die "Can't create file named bootlog.out: $!\n";
while (<BOOTLOG>)
{
print LOGOUT $_;
}
close (LOGOUT);
close (BOOTLOG);
open(BOOTLOG,“boot.log”)| | die“无法打开名为boot.log:$!”的文件;
打开(注销“>bootlog.out”)| | die“无法创建名为bootlog.out:$!\n”的文件;
而()
{
打印注销$;
}
关闭(注销);
关闭(引导日志);
注意:建议不要使用裸字文件句柄。我更愿意将上述代码重写如下:
use strict;
use warnings;
open my $fh_boot_log, '<', 'boot.log' or die "Can't open file 'boot.log': $!";
open my $fh_log_out, '>', 'bootlog.out' or die "Can't create file 'bootlog.out': $!\n";
while (<$fh_boot_log>)
{
print $fh_log_out $_;
}
close $fh_log_out;
close $fh_boot_log;
使用严格;
使用警告;
打开我的$fh_boot_日志,“只需使用输出文件句柄注销
以及打印
。在实际打印到输出文件句柄之前,还需要打开输出文件句柄
open (BOOTLOG, "boot.log") || die "Can't open file named boot.log: $!";
open (LOGOUT, ">bootlog.out") || die "Can't create file named bootlog.out: $!\n";
while (<BOOTLOG>)
{
print LOGOUT $_;
}
close (LOGOUT);
close (BOOTLOG);
open(BOOTLOG,“boot.log”)| | die“无法打开名为boot.log:$!”的文件;
打开(注销“>bootlog.out”)| | die“无法创建名为bootlog.out:$!\n”的文件;
而()
{
打印注销$;
}
关闭(注销);
关闭(引导日志);
注意:建议不要使用裸字文件句柄。我更愿意将上述代码重写如下:
use strict;
use warnings;
open my $fh_boot_log, '<', 'boot.log' or die "Can't open file 'boot.log': $!";
open my $fh_log_out, '>', 'bootlog.out' or die "Can't create file 'bootlog.out': $!\n";
while (<$fh_boot_log>)
{
print $fh_log_out $_;
}
close $fh_log_out;
close $fh_boot_log;
使用严格;
使用警告;
打开我的$fh\u启动日志,“使用magic的另一个解决方案
:
#!/usr/bin/env perl
use strict; use warnings;
while (<>) {
print;
}
#/usr/bin/env perl
严格使用;使用警告;
而(){
印刷品;
}
在以下情况下使用:
$perl script.ploutput.txt
另一种使用magic的解决方案
:
#!/usr/bin/env perl
use strict; use warnings;
while (<>) {
print;
}
#/usr/bin/env perl
严格使用;使用警告;
而(){
印刷品;
}
在以下情况下使用:
$perl script.ploutput.txt
我才刚刚开始,我正在努力使我的代码尽可能简单。谢谢您的帮助,但您给出的建议不会创建带有记录的输出文件。@LMN0321是否有任何错误?不,我没有收到错误,只是没有创建输出文件。我添加了一个额外的行,这似乎有效,但我不确定它的语法是否正确,或者它是否真的写入了文件。粘贴在注释中的代码将不可读。在你的问题中更新它。我没有理由解释我上面的代码不起作用。@LMN0321那个编辑看起来不错。它会将输入文件内容打印到输出文件以及术语。我不明白你的问题。我才刚刚开始,我正试图让我的代码尽可能简单。谢谢您的帮助,但您给出的建议不会创建带有记录的输出文件。@LMN0321是否有任何错误?不,我没有收到错误,只是没有创建输出文件。我添加了一个额外的行,这似乎有效,但我不确定它的语法是否正确,或者它是否真的写入了文件。粘贴在注释中的代码将不可读。在你的问题中更新它。我没有理由解释我上面的代码不起作用。@LMN0321那个编辑看起来不错。它会将输入文件内容打印到输出文件以及术语。我不明白你的问题。总是:严格使用;使用警告代码>。始终:严格使用;使用警告代码>。