Thread Textfile automatisiert editieren (2 answers)
Opened by wotuzu17 at 2012-01-12 10:49

wotuzu17
 2012-01-12 15:48
#155414 #155414
User since
2011-06-14
5 Artikel
BenutzerIn
[default_avatar]
Herzlichen Dank, GwenDragon! Es funktioniert bestens.

Hab mir mit meinem rudimentären Perl Kenntnissen eine kleine before/after Ausgabe dazugebastelt:

Code: (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/perl
use strict;
use warnings;

sub print_file_content {
my $filename = $_[0];
my $fh;
open( $fh, "<", "$filename" ) or die "Cant open $filename: $!";
my @daten = <$fh>;
close($fh);
print "file $_[1] :\n";
for my $i ( 0 .. @daten - 1 ) {
if ($daten[$i] =~ /^Date:/ | $daten[$i] =~ /^Version:/) {
print " $daten[$i]";
}
}
}

use Fcntl ':flock';

my $filename = "candlesticks/pkg/DESCRIPTION";
print_file_content($filename, "before");

my $fh;
open( $fh, "+<", "$filename" ) or die "Cant open $filename: $!";
flock( $fh, 0 );
my @daten = <$fh>;
my ( $number, $date );
my $changed;
for my $i ( 0 .. @daten - 1 ) {
if ( $daten[$i] =~ /^Date:\s+\d{4}-\d\d?-\d\d?/ ) {
my @datum = ( localtime(time) )[ 3 .. 5 ];
$daten[$i] = sprintf(
"Date: %4d-%02d-%02d\n",
$datum[2] + 1900,
$datum[1] + 1,
$datum[0]
);
$changed++;
}
elsif ( $daten[$i] =~ /^Version:\s+\d+\.\d+\-\d+$/ ) {
$daten[$i] =~ s/^(Version:\s+\d+\.\d+\-)(\d+)$/"$1".(1+$2)/e;
$changed++;
}
}
if ($changed) {
seek( $fh, 0, 0 ) or die "Can't rewind $filename: $!\n";
truncate( $fh, 0 );
print $fh @daten;
}
close($fh) or die "Cant close $filename: $!";

print_file_content($filename, "after");
__END__

View full thread Textfile automatisiert editieren