开发者

How to update a set of original files using Perl?

I would like to take an array of the original files and update them.. This means I would like to use the output_files as the input_files for the updates

For example:

a_all.txt ---update to---> a2_all.txt ---update to--> a3_all.txt ---update to-- a3_all.txt

next in the array

b_all.txt ---update to---> b2_all.txt ---update to--> b3_all.txt ---update to-- b3_all.txt

etc..to z_all.txt

Here's the code I started so far but I'm at a loss how to fin开发者_Go百科ish it..

#!/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',>>
}


Here's a better idea:

#!/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;
}

I don't see you as making changes to what is being written out, so it sounds like you just want to dump ${letter}_all.txt out to ${letter}${num}_all.txt.

Or if you wanted to know how to do a more sane version of standard perl, I've made the following changes.

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

My version of what you put in the comments, would be more like this:

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

But I haven't tested this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜