2014-12-29 20 views
1

我想創建一個腳本,從網站獲取日誌文件(內容),然後將其輸入到文本文件,但我有錯誤,如果使用嚴格存在:Perl獲取網頁內容,然後將其寫入文本文件

不能使用的字符串(「/ home/user中/下載/文」)爲標誌裁判而「嚴格裁判」在使用中./scriptname線92

而且通過移除使用嚴格的:我得到的另一個錯誤是:

文件名過長的./scriptname線91

我試過Perl: Read web text file and "open" it

但是,它沒有爲我工作。另外我是Perl的新手和Perl語法的混淆。

是否有任何建議或建議可用?

注意:該代碼確實存在與RoomOutProcessTT存在的整條線並將其顯示多次。

這是代碼。

my $FOutput = get "http://website/Logs/Log_number.ini"; 
my $FInput = "/home/User/Downloads/text"; 
open $FInput, '<', $FOutput or die "could not open $FInput: $!"; 
my $ctr; 
my @results; 
my @words = <$FInput>; 
@results = grep /RoomOutProcessTT/, @words; 
print "@results\n"; 
close $FInput; 

open $FInput, '<', $FOutput or die "could not open $FInput: $!"; 
while(<$FInput>){ 
    $ctr = grep /RoomOutProcessTT/, split ' ' , $_;    
    $ctr += $ctr; 
} 
print "RoomOutProcessTT Count: $ctr\n"; 
close $FInput; 
+1

「open」的第一個參數是文件句柄的名稱,而不是文件名本身。 '<'意味着打開指定的文件名後的文件名爲READ,並使用文件句柄名稱來引用它。你使用'open'是不正確的。見[參考](http://perldoc.perl.org/functions/open.html) –

+0

你是說我應該爲filehandler創建另一個變量?我也這樣做了,但它會產生另一個錯誤,即「無法打開GLOB(0x17252b0):文件名太長,在./scriptname第92行。」我迷路了。 – galbert

+0

這不是一個變量,它是一個裸號標識符,用於與您打開的文件交互,無論是閱讀還是書寫。以我的答案爲例。 –

回答

0

打開的第一個參數是文件句柄名稱,而不是文件的實際名稱。稍後在open函數中。

更改您的代碼:

my $FOutput = get "http://website/Logs/Log_number.ini"; # your content should be stored in this 
                 # variable, you need to write data to your output file. 
my $FInput = "/home/User/Downloads/text"; 
open OUTPUT_FILEHANDLE, '>', $FInput or die "could not open $FInput: $!"; # give a name to the file 
                      # handle, then supply the file name itself after the mode specifier. 
                      # You want to WRITE data to this file, open it with '>' 
my $ctr; 
my @results; 
my @words = split(/(\r|\n)/, $FOutput); # create an array of words from the content from the logfile 
             # I'm not 100% sure this will work, but the intent is to show 
             # an array of 'lines' corresponding to the data 

# here, you want to print the results of your grep to the output file 
@results = grep /RoomOutProcessTT/, @words; 
print OUTPUT_FILEHANDLE "@results\n"; # print to your output file 
# close the output file here, since you re-open it in the next few lines. 
close OUTPUT_FILEHANDLE; 

# not sure why you're re-opening the file here... but that's up to your design I suppose 
open INPUT_FILEHANDLE, '<', $FInput or die "could not open $FInput: $!"; # open it for read 
while(<INPUT_FILEHANDLE>){ 
    $ctr = grep /RoomOutProcessTT/, split ' ' , $_;    
    $ctr += $ctr; 
} 
print "RoomOutProcessTT Count: $ctr\n"; # print to stdout 
close INPUT_FILEHANDLE; # close your file handle 

我可能會建議您切換使用來識別「輸入和輸出」的條款,因爲它是有點混亂。在這種情況下,輸入實際上是從網絡中提取的文件,輸出是您的文本文件。至少我是這麼解釋的。你可能想在你的最終設計中解決這個問題。

+0

謝謝你的提示,正如你所說我認爲我正在生成一個輸出,我正在使用文件處理程序錯誤......並且還將使用您的示例作爲參考。將測試,然後調整代碼一段時間 – galbert

+0

@galbert好,因爲這不再是真的與原來的問題,我想我只能去儘可能引導你在你需要的方向...多少你期望從遠程位置文件得到結果嗎?你確定你的算法是正確的嗎?如何「巨大」是巨大的?想想更多的調試,它應該讓你到你需要的地方。祝你好運。 –

+0

您的指南確實有幫助Ryan J.只要有一個名爲RoomOutProcessTT的現有文本在那裏...是的,我認爲是因爲我創建了分割和征服每個腳本,這就是爲什麼我可以說我的算法是正確的。 。現在可以,但是,我現在的問題是內容正在輸出,我不希望這樣做仍然在尋找一種方法來做到這一點...... – galbert

相關問題