Perl在文件中的读写
好的,我回来问另一个问题。我知道在Python中,有一种方法可以读取文件,而无需指定它将是哪个文件,直到您在命令提示符下。所以基本上你可以设置脚本,这样你就可以读入任何你想要的文件,而不必每次都回去修改编码。有没有办法在Perl中实现这一点?如果是这样,你也能写这样的文件吗?谢谢 这就是我所拥有的:Perl在文件中的读写,perl,Perl,好的,我回来问另一个问题。我知道在Python中,有一种方法可以读取文件,而无需指定它将是哪个文件,直到您在命令提示符下。所以基本上你可以设置脚本,这样你就可以读入任何你想要的文件,而不必每次都回去修改编码。有没有办法在Perl中实现这一点?如果是这样,你也能写这样的文件吗?谢谢 这就是我所拥有的: open (LOGFILE, "UNSUCCESSFULOUTPUT.txt") or die "Can't find file"; open FILE, ">", "outpu
open (LOGFILE, "UNSUCCESSFULOUTPUT.txt") or die "Can't find file";
open FILE, ">", "output.txt" or die $!;
while(<LOGFILE>){
print FILE "ERROR in line $.\n" if (/Error/);
}
close FILE;
close LOGFILE;
open(日志文件,“unsuccessfuldoutput.txt”)或die“找不到文件”;
打开文件“>”、“output.txt”或die$!;
while(){
如果(/ERROR/),则打印文件“第$行出错。\n”;
}
关闭文件;
关闭日志文件;
这就是我所拥有的nome:
#!/usr/local/bin/perl
my $argument1 = $ARGV[0];
open (LOGFILE, "<$argument1") or die "Can't find file";
open FILE, ">>output.txt" or die $!;
while(<LOGFILE>){
print FILE "ERROR in line $.\n" if (/Error/);
}
close FILE;
close LOGFILE;
#/usr/local/bin/perl
我的$argument1=$ARGV[0];
open(LOGFILE,“命令行参数在中提供。您可以随意使用它们,包括将它们作为文件名传递给open
my ($in_qfn, $out_qfn) = @ARGV;
open(my $in_fh, '<', $in_qfn ) or die $!;
open(my $out_fh, '>', $out_qfn) or die $!;
print $out_fh $_ while <$in_fh>;
用法:
perl script.pl UNSUCCESSFULOUTPUT.txt >output.txt
如果使script.pl
可执行(chmod u+x script.pl
),则可以从命令中删除perl
。我假设您询问如何将参数传递给perl脚本。这是通过
您还可以使用菱形运算符的魔力,它将以文件的形式打开脚本的参数,或者如果没有提供参数,则使用STDIN。菱形运算符用作普通的文件句柄,通常是while()…
预计到达时间:
使用您提供的代码,您可以通过以下方式使其更加灵活:
use strict;
use warnings; # always use these
my $file = shift; # first argument, required
my $outfile = shift // "output.txt"; # second argument, optional
open my $log, "<", $file or die $!;
open my $out, ">", $outfile or die $!;
while (<$log>) {
print $out "ERROR in line $.\n" if (/Error/);
}
这就是我相信你想要的:
#!usr/bin/perl
my $argument1 = $ARGV[0];
open (LOGFILE, "<$argument1") or die "Can't find file";
open (FILE, ">output.txt") or die $!;
while(<LOGFILE>){
print FILE "ERROR in line $.\n" if (/Error/);
}
close FILE;
close LOGFILE;
要附加更改此行,请执行以下操作:
open (FILE, ">output.txt") or die $!;
与之极为相似的是:
open (FILE, ">>output.txt") or die $!;
好的,我把我当前的代码放在原来的问题中,你能告诉我如何在其中使用argv吗?谢谢!只要用一个变量替换硬编码的文件名,并按照我在回答中显示的那样设置变量。例如,my$file=shift;打开我的$log,”@用户1440061,在我的答案底部为您更新的问题添加了答案。好的,我更改了它,现在它创建了一个新文件,但其中没有写入任何内容。如果没有写入任何内容,那是因为在UNSUCCESSFULOUTPUT.txt
中没有包含错误的行。它是如何编码的?很抱歉进行了多次编辑,并且使用mytxt.txt是不成功的输入文件。这正是我所需要的!现在,我很好奇,是否有必要将正在写入的信息添加到一个文件中,而不是替换它?它不是附加的。将我的最新代码添加到原始问题中。嗯,您发布的代码在我的机器上工作,哦!您能运行“perl-v”吗从命令行中,我们可以看到您有什么版本?可能惯例已经改变了?
use strict;
use warnings; # always use these
my $file = shift; # first argument, required
my $outfile = shift // "output.txt"; # second argument, optional
open my $log, "<", $file or die $!;
open my $out, ">", $outfile or die $!;
while (<$log>) {
print $out "ERROR in line $.\n" if (/Error/);
}
grep -n Error input.txt > output.txt
#!usr/bin/perl
my $argument1 = $ARGV[0];
open (LOGFILE, "<$argument1") or die "Can't find file";
open (FILE, ">output.txt") or die $!;
while(<LOGFILE>){
print FILE "ERROR in line $.\n" if (/Error/);
}
close FILE;
close LOGFILE;
> perl nameofpl.pl mytxt.txt
open (FILE, ">output.txt") or die $!;
open (FILE, ">>output.txt") or die $!;