2012-06-06 83 views
2

好的,所以我回来了与另一个问题。我知道在Python中,有一种方法可以在文件中读取,而不必指定它将是哪个文件,直到您进入命令提示符。所以基本上你可以设置脚本,这样你就可以读取你想要的任何文件,并且不必每次都回去修改编码。有没有办法在Perl中做到这一点?如果是这样,你能写这样的文件吗?谢谢。Perl读取和写入文件

这是我有:

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;     

这是我诺姆:

#!/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; 

,它仍然没有附加...在@ARGV提供

回答

2

这是什么,我相信你想要的:

#!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 $!; 
+0

抱歉无数次编辑,并且mytxt.txt是不成功的输入文件。 – PinkElephantsOnParade

+0

这正是我需要的!现在,只是好奇,是否有将正在写出的信息添加到文件而不是替换它? – user1440061

+0

刚刚使用该信息进行编辑。 – PinkElephantsOnParade

3

的命令行参数。你可以随心所欲地将它们作为文件名传递给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>; 

但这并不是一种非常unixy的做事方式。在Unix的习惯,下面将通过命令行上指定的每个文件,一行一次读:

while (<>) { 
    ... 
} 

输出通常是通过重定向放置在文件中。

#!/usr/bin/env perl 
# This is mycat.pl 
print while <>; 

# Example usage. 
mycat.pl foo bar > baz 

# Edit foo in-place. 
perl -i mycat.pl foo 

唯一一次一个通常触摸@ARGV是处理选项,即使是这样,一个通常使用的Getopt::Long代替直接触摸@ARGV


关于你的代码,你的脚本应该是:

#!/usr/bin/env perl 
while (<>) { 
    print "ERROR in line $.\n" if /Error/; 
} 

用法:

perl script.pl UNSUCCESSFULOUTPUT.txt >output.txt 

如果你让script.pl可执行文件(chmod u+x script.pl)你可以从命令摆脱perl

+0

@ user1440061,新增的答案在我的回答底部更新的问题。 – ikegami

+0

好吧,我改变它,现在它创建一个新的文件,但没有写入它。 – user1440061

+0

如果没有写入任何内容,那是因为'UNSUCCESSFULOUTPUT.txt'中没有包含'Error'的行。它是如何编码的? – ikegami

2

我假设你问如何将参数传递给perl脚本。这与@ARGV variable.

use strict; 
use warnings; 

my $file = shift; # implicitly shifts from @ARGV 
print "The file is: $file\n"; 

您还可以使用钻石操作<>,这将打开参数传递给脚本文件,或者使用标准输入,如果没有提供参数的魔法造成的。钻石运营商作为一个正常的文件句柄,通常while (<>) ...

ETA:

您所提供的代码,你可以把它更加灵活通过这样做:

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/); 
} 

另请参阅ikegami的答案是如何让它更像其他Unix工具,例如接受STDIN或文件参数,并打印到STDOUT。

正如我在你前面的问题发表了评论,你可能只是想用这份工作已经存在的工具:

grep -n Error input.txt > output.txt 
+0

好吧,我把我当前的代码放在原始问题中,你能告诉我如何使用argv吗?谢谢! – user1440061

+0

只需用变量替换硬编码的文件名,然后按照我在答案中显示的那样设置变量。例如。 '我的$ file = shift;打开我的$日志,“<”,$文件或死亡$ !;' – TLP