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

GwenDragon
 2012-01-12 14:38
#155412 #155412
User since
2005-01-17
14757 Artikel
Admin1
[Homepage]
user image
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
39
40
41
use strict;
use warnings;

use Fcntl ':flock';

my $filename = shift || die "No filename!";
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: $!";

__END__

Package: Packagename
Date: 2012-01-10
Version: 0.3-1
Licence: GPL-3

Last edited: 2012-01-12 14:39:26 +0100 (CET)

View full thread Textfile automatisiert editieren