Leser: 1
|< 1 2 >| | 12 Einträge, 2 Seiten |
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#!/usr/bin/perl -n BEGIN { use strict; use warnings; #use Data::Dumper; my $filename = $ENV{FILENAME}; unless (defined($filename)) {die "No filename in environment (filename)\n";} @ARGV = $filename; my $flag = '0'; my @lines; my $whole_match; my @part_before_match; my @part_after_match; my @number_of_matches; } if ( $_ =~ m/<Text>/ ) { $flag = '1'; } if ( $flag == '1' ) { $_ =~ s/\r\n//; $_ =~ s/\n//; $whole_match = $whole_match . $_ . " "; if ( $_ =~ m/<\/Text>/ ) { $flag = '0'; @number_of_matches = $whole_match =~ m/(Text>)/g; if ( scalar(@number_of_matches) > 2 ) { die "More then one Text in one line."; } @part_before_match = $whole_match =~ /(.*)(?=<Text>)/; @part_before_match->[0] =~ s/^ *$//g; @part_after_match = $whole_match =~ /(?<=<\/Text>)(.*)/; @part_after_match->[0] =~ s/^ *$//g; $whole_match =~ s/[^<]*<Text>//; $whole_match =~ s/<\/Text>.*//s; @lines = $whole_match =~ m/(.{1,4})/g; if ( defined @part_before_match && @part_before_match->[0] ne "") {print "@part_before_match\n";} #print Dumper @part_before_match; foreach my $line (@lines) { print " <Text>$line</Text>\n"; } if ( defined @part_after_match && @part_after_match->[0] ne "") {print "@part_after_match\n";} #print Dumper @part_after_match; undef @lines; undef $whole_match; undef @part_before_match; undef @part_after_match; undef @number_of_matches; } } else { print; }
styx-cc+2008-12-21 13:44:24--Hm, jetzt glaub ichs zu sehen, du willst immer 4 Zeichen zwischen <Text> und </Text> haben?
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
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
local $/ = '';
my $data = <DATA>;
do {
$data =~ m|(<Text>.+</Text>)|s;
my $prepared_string = prepare_string($1);
$data =~ s/$1/$prepared_string/s;
} while($1);
print Dumper $data;
## subs ##
sub prepare_string {
my $string = shift;
$string =~ s/\n|<Text>|<\/Text>//g;
my $chunk_size = 4;
my $prepared_string;
for (my $i = 0; $i <= length($string); $i=$i+4) {
my $part = substr($string, $i, $chunk_size);
$prepared_string .= "<Text>$part</Text>\n";
}
return $prepared_string;
}
__DATA__
<Feld><Text>Hallo liebe Perl-Gemeinde,
ich wünsche euch ein schönes Fest und
einen guten Rustch ins neue
Jahr.</Text></Feld><Sig>
abc</Sig>
1
2
3
4
5
6
7
8
9
10
11
12
13
stefan@stefan-laptop:~/perl$ perl sort.pl
$VAR1 = '<Feld><Text>Hall</Text>
<Text>o li</Text>
<Text>ebe </Text>
<Text>Perl</Text>
[...]
<Text>ins </Text>
<Text>neue</Text>
<Text>Jahr</Text>
<Text>.</Text>
</Feld><Sig>
abc</Sig>
';
1 2 3 4 5
@match = $data =~ m|(<Text>[^<]*</Text>)|sg; foreach my $m (@match) { my $prepared_string = prepare_string($m); $data =~ s/$m/$prepared_string/s; }
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 39 40
#!/usr/bin/perl use strict; use Data::Dumper; use warnings; local $/ = undef; my $data = <DATA>; my @match; @match = $data =~ m|(<Text>[^<]*</Text>)|sg; foreach my $m (@match) { my $prepared_string = prepare_string($m); $data =~ s/$m/$prepared_string/s; } print $data; ## subs ## sub prepare_string { my $string = shift; $string =~ s/\n|<Text>|<\/Text>//g; my $chunk_size = 10; my $prepared_string; for (my $i = 0; $i <= length($string); $i=$i+$chunk_size) { my $part = substr($string, $i, $chunk_size); $prepared_string .= "<Text>$part</Text>\n"; } return $prepared_string; } __DATA__ <Feld><Text>Hallo liebe Perl- Geme(i)nde. diesmal mit leerzeile </Text></Feld><Sig> abc</Sig> <Text>V[iel]e Grüße leo11</Text><Text>ps.: schönen abend</Text>
$data =~ s/$m/$prepared_string/s;
if ( {<Text>} .. {</Text>}
|< 1 2 >| | 12 Einträge, 2 Seiten |