2011-04-01 12 views
1

我想借原始文件的數組,更新它們。這意味着,我想用output_files作爲input_files的更新如何使用Perl更新一組原始文件?

例如:

a_all.txt - - 更新到---> a2_all.txt ---更新到 - > a3_all.txt ---更新到 - a3_all.txt
接下來在數組中
b_all.txt ---更新到 - - > b2_all.txt ---更新 - > b3_all.txt ---中場休息更新b3_all.txt
etc..to z_all.txt

這裏是我開始至今的代碼,但我不知如何去完成它..

#!/usr/bin/perl 

use warnings; 
use strict; 

my $_arry_counter = 1; 

my @input_files_1 = qw(a b c d e f g h i j k l m n o p q r s t u v w x y z); 

foreach my $file_names (@input_files${_array_counter}) { 
    print open 'INPUT_FILES','<',"/home/${file_names}_all.txt" or die "unable to open file !"; 

    while(<INPUT_FILES>) { 
     open 'INPUT_FILES','>>',"/home/$file_count\ 
    } 

    my @input_files_\$_ = open 'INPUT_FILES',>> 
} 
+0

你能解釋(或固定)a3_all.txt - >您的要求設置的a3_all.txt一部分?我懷疑你打算給出4的錯字,但我們需要知道。 (可以說是一個箭頭'>'也是如此。) – 2011-04-01 15:53:22

+0

不要在文件句柄周圍加引號!更妙的是,使用詞法文件句柄和3-arg open(),這個錯誤首先不會發生。 – tadmc 2011-04-01 21:27:54

回答

1

這裏有一個更好的主意:

#!/usr/bin/perl 

use warnings; 
use strict; 
use File::Slurp qw<append_file read_file>; 

my @input_files = ('a'..'z'); 

foreach my $file_name (@input_files) { 
    my $contents = read_file("/home/${file_name}_all.txt"); 
    append_file("/home/${file_name}${_}_all.txt", $contents) foreach 2..3; 
} 

我沒有看到你作爲對正在寫出的內容進行更改,因此您聽起來像只想將${letter}_all.txt轉儲到${letter}${num}_all.txt

或者,如果你想知道怎麼做標準的Perl更理智的版本,我做了以下更改。

use warnings; 
use strict; 
use English qw<$OS_ERROR>; 

my $_arry_counter = 1; 
# no reason to write the whole alphabet 
my @input_files_1 = 'a'..'z'; 

# I'm not sure what you thought you could do with your foreach loop expression. 
foreach my $file_names (@input_files_1) { 
    # save! the path 
    my $path = "/home/${file_names}_all.txt"; 
    # open *lexical* handles and die with *explicit* message and OS error. 
    open my $input,'<', $path or die "unable to open file '$path'! - $OS_ERROR"; 

    # best way to do this to minimze IO is to write two files at once to two 
    # two different handles. 
    my @outs 
     = map { 
      # repeating practice with inputs 
      my $out_path = "/home/${file_names}$_\_all.txt"; 
      open my $out, '>>', or die "unable to open file '$out_path'! - $OS_ERROR"; 
      $out; 
     } 2..3; 
    while (my $line = <$input>) { 
     # in this foreach $_ is a handle, so we need to save the line in a var. 
     print $_ $line foreach @outs; 
    } 
    # close each output 
    close $_ foreach @outs; 
    # close current input 
    close $input; 
} 

我的,你放什麼意見的版本,會更喜歡這樣的:

use English qw<$RS>; # record separator -> $/ 

my $regex = qr{ 
     (^ \@ .*) 
     # note that I turn on s for *select* sections 
     ((?s) .*?) 
     (^ (windows|linux) .*) 
     ((?s) .*?) 
     (^ (windows|linux) .*) 
     ((?s) .*?) 
    }mx; 
foreach my $file_name (@input_files) { 
    my $contents = read_file("/home/jbutler/final/${file_name}_all.txt"); 
    local $RS = 'Data'; 
    $contents =~ s/$regex/$1$2­$3$4\n__Data__\n$1\n$5$6/m; 
    append_file("/home/jbutler/final/${file_name}${_}_all.txt", $contents) foreach 2..3; 
} 

但是我沒有測試過這一點。

+0

謝謝Axeman,我遺漏了更新代碼,但它進入了一個while循環,它遍歷每一行文件。 – 2011-04-01 15:41:51

+0

#在/ usr/bin中/ perl的 使用警告!; 使用嚴格; 使用File :: Slurp qw ; $/='__ Data__'; my @input_files =('a'..'z'); 我的foreach $ FILE_NAME(@input_files) { 而(<$file_name>) { $ _ =〜S /(^ \ @ [^ \ n] *)(^(窗口(*?)| Linux版) [^ \ n]的*)(?*)(^(視窗| Linux)的[^ \ n]的*)(*)/ $ 1 $ 2 $ 3 $ 4 \ n__Data __ \ N $ 1 \ N $ $ 5 6 /毫秒。 my $ contents = read_file(「/home/jbutler/final/${file_name}_all.txt」); append_file( 「/home/jbutler/final/${file_name}${_}_all.txt」,$內容)的foreach 2..3; }} – 2011-04-01 15:56:47

+0

將這項工作使用添加的代碼...... – 2011-04-01 15:57:19

相關問題