2012-06-06 79 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