2014-03-04 33 views
1

我是新來的perl。我正在編寫這個腳本,它應該從包含完整日誌變化的日誌文件中獲取爲數據庫中的特定表記錄的任何類型的更改。這些特定表中的變化應該存儲在一個新文件中(目前我正在使用.txt文件,但實際上必須在瀏覽器中顯示,但下一步就是這樣)。空間被排除在perl輸出

所以我的問題是輸出得到存儲,因爲我希望但所有的空間丟失,因此輸出顯示爲一個字符串,沒有空格。任何人都可以幫忙嗎?我該如何在詞語之間傳播空間?

另外在調試時,我發現(根據我的意思),這是因爲「while(< @file>)」,因爲此行將整個文件作爲一個字符串,沒有空格。所以任何人都可以爲此提出一些替代或解決方案。

腳本是:

#!/usr/bin/perl -w 
use strict; 
use warnings; 

#take the file as input, (if want from command line use ARGV[0]) 
open(FILE, "</home/apareek/sample.txt") or die '\nfile not opened $!\n'; 
open(my $out, ">/home/apareek/output.txt") or die ":P :P"; 

my $line; 
my @file = <FILE>; 

#take table name as input 
print "\nEnter the table name you want to make query for\n"; 
my $tbl_name = <STDIN>; 
chomp($tbl_name); 
my $new_tabname = $tbl_name . "..."; 

print "\nTable Name: $new_tabname\n"; 
if (grep(m/$new_tabname/, @file) eq 1) { 
    print "Table name exsts.\n"; 
    while(<@file>) {       
     if (/^$tbl_name/ .. /^Check/) { 
      if($_ !~ /$new_tabname/ and $_ !~ /Checking/) { 
       print $out $_; # $out (is the handler of output file) 
      } 
     } 
    } 
} 

輸入數據:

Checking table BOARD_EQSL34V... 
Checking table CFMGlobalTbl... 
Checking table CFMMaTable... 
!!!!!! Diff001: differences in record definition section 
!!!!!! mdIdx : Record definition type differs Unsigned Int(4 Byte)(db1) vs. (db2) 
Checking table CFMMdTable... 
!!!!!! Diff002: differences in record definition section 
!!!!!! mdIdx : Record definition type differs : Unsigned Int (4 Byte) (db1) vs. (db2) 
Checking table CFMMepListTbl... 
Checking table CFMMepTable... 
+0

你可能會發布一段輸入數據,以及如何將它讀入'@ file'? – Fabricio

+0

我已經包含了輸入數據。和腳本的額外部分,以便您可以瞭解如何將它讀入@file。 – ashish

+0

你期望'(<@file>){...}'做什麼?你的意思是爲(@file){...}'? – Borodin

回答

0

你有這樣的兩種不同方式的組合。 要麼你閱讀整個文件到一個數組去

my @file = <FILE>; 
    .... 
    for (@file) { 

或者你讀文件的一行在同一時間(這是最好,如果該文件可能是大)

while (<FILE>) { 

我會將其寫爲

my $filename = '/home/apareek/sample.txt' ; 
    open my $file , '<', $filename or die 'file $filename not opened $!\n'; 
    while (<$file>) { 

查看Why is three-argument open calls with autovivified filehandles a Perl best practice?的原因。

+0

如果我正在編寫i.r. 「while(<$file>)」它在運行時給出的錯誤是「readline()在./str_parse2.pl未開啓的文件句柄上」 – ashish