Thread mehre file dynamisch rauschreiben (6 answers)
Opened by gast at 2010-05-04 12:59

esskar
 2010-05-04 13:49
#136729 #136729
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
hier mal nen ansatz (ungetestet)
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
!/usr/bin/perl
use strict;
use warnings;

my @output = (
        { file => "chr1.gens", pattern => qr/^0/ },
        { file => "chr2.gens", pattern => qr/^1/ },
        { file => "chr3.gens", pattern => qr/^2/ },
);

my $file = "test.txt";
open(IN,'<'.$file) || die "Can not open file $file: $!";

# open the output handles
foreach my $o (@output) {
        my $file = $o->{file};
        open(my $handle, "> $file") or die "Can't open $file: $!";
        $o->{handle} = $handle;
}

while (my $line = <IN>) {
                
        foreach my $o (@output) {
        
                my $pattern = $o->{pattern};
                if($line =~ $pattern) {
                        print $o->{handle} $line;
                        # oder eben print $o->{handle} "$line\n";
                        # falls das zusätzlich \n erwünscht ist
                }
        }
                
}

# close the output handles
foreach my $o (@output) { close $o->{handle}; }

close IN;

View full thread mehre file dynamisch rauschreiben