【问题标题】:How to update a set of original files using Perl?如何使用 Perl 更新一组原始文件?
【发布时间】:2014-05-02 12:14:51
【问题描述】:

我想获取原始文件的数组并更新它们。这意味着我想使用 output_files 作为更新的 input_files

例如:

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',>>
}

【问题讨论】:

  • 您能解释(或修复)a3_all.txt --> a3_all.txt 部分的需求设置吗?我怀疑你想要 4 的地方有 3 的错字,但我们需要知道。 (可以说也缺少一个箭头“>”。)
  • 不要在文件句柄周围加上引号!更好的是,使用词法文件句柄和 3-arg open(),这个错误一开始就不会发生。

标签: perl


【解决方案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;
}

你在 cmets 中的我的版本会更像这样:

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

但我还没有测试过。

【讨论】:

  • 感谢 Axeman,我省略了更新代码,但它进入了一个遍历文件每一行的 while 循环。
  • #!/usr/bin/perl 使用警告;使用严格;使用 File::Slurp qw; $/='数据';我的@input_files = ('a'..'z'); foreach my $file_name (@input_files) { while () { $_ =~ s/(^\@[^\n]*)(.*?)(^(windows|linux)[^\n ]*)(.*?)(^(windows|linux)[^\n]*)(.*)/$1$2$3$4\n__Data__\n$1\n$5$6/ms;我的 $contents = read_file("/home/jbutler/final/${file_name}_all.txt"); append_file("/home/jbutler/final/${file_name}${_}_all.txt", $contents) foreach 2..3; } }
  • 是否可以使用您的加载项代码#!/usr/bin/perl 使用警告;使用严格;使用 File::Slurp qw; $/='数据';我的@input_files = ('a'..'z'); foreach my $file_name (@input_files) { while () { $_ =~ s/(^\@[^\n]*)(.*?)(^(windows|linux)[^\n ]*)(.*?)(^(windows|linux)[^\n]*)(.*)/$1$2$3$4\n__Data__\n$1\n$5$6/ms;我的 $contents = read_file("/home/jbutler/final/${file_name}_all.txt"); append_file("/home/jbutler/final/${file_name}${_}_all.txt", $contents) foreach 2..3; } }
  • 哇,谢谢一百万,您的解决方案非常完美,我所要做的就是安装 File::Slurp 模块,它按要求工作。再次感谢 Axeman
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-16
  • 1970-01-01
  • 2010-09-16
  • 2020-05-10
  • 2012-01-01
  • 2016-06-05
  • 1970-01-01
相关资源
最近更新 更多