Jemand zu Hause?Leser: 22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/perl
my $IN;
open ($IN,'</test.txt') ;
while (<$IN>){
if (/TESTWORT1/ .. /\-\-\-/) {
print_log "TESTWORT1 found";
}
else {
print_log "TESTWORT1 notfound";
}
}
Wie frage ich & perlintro
brian's Leitfaden für jedes Perl-Problem
File-ChangeNotify mit dem du eine Dateiänderung überwachen kannst.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/perl
my $IN;
open ($IN,'</test.txt') ;
while (<$IN>){
if (/TESTWORT1/ .. /\-\-\-/) {
print_log "TESTWORT1 found";
system 'echo 0 > /test.txt';
seek $IN, 0, SEEK_SET;
}
else {
print_log "TESTWORT1 notfound";
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#!/usr/bin/perl use strict; use warnings; my $active=10; while (1) { if ($active) { print "Active\n"; $active--; } else { print "Inactive\n"; last(); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#!/ust/bin/perl use strict; use warnings; my $file='./test.txt'; open (my $in, '+<', $file) or die("ERROR OPEN $file ($!)\n"); while(<$in>) { if(/TESTWORT1/ .. /---/) { print "TESTWORT1 found\n"; seek($in, 0,0); truncate($in,0); } else { print "TESTWORT1 not found\n"; } } close($in); print "EXIT\n";
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
TESTWORT1 not found
TESTWORT1 not found
TESTWORT1 not found
TESTWORT1 not found
TESTWORT1 not found
TESTWORT1 not found
TESTWORT1 not found
TESTWORT1 not found
TESTWORT1 not found
TESTWORT1 not found
TESTWORT1 not found
TESTWORT1 not found
TESTWORT1 not found
TESTWORT1 not found
TESTWORT1 found
EXIT
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
#!/usr/bin/perl use strict; use warnings; my $file='./test.txt'; open (my $fh, '+<', $file) or die("ERROR OPEN $file ($!)\n"); while(<$fh>) { if(/TESTWORT1/ .. /---/) { print "TESTWORT1 found\n"; # Aktuelle Dateigröße minus der Länge der Zeile my $size = (-s $fh) - length($_); # aktuelle Positon minus der Länge der Zeile my $pos=tell($fh) - length($_); # keine Zeilentrenner local $/=undef; # Alle daten auf einmal einlesen my $data=<$fh>; # zur Gespeicherten position springen seek($fh,$pos,0); # Daten schreiben, damit ist die aktuelle Zeile Gelöscht print $fh $data; # zum Anfang der datei springen seek($fh, 0,0); # Dateigröße anpasen truncate($fh,$size); } } close($fh); print "EXIT\n";
Tie::File zurück greifen, das macht das ganze Gehampel mit truncate, seek und kopieren von sich aus transparent im Hintergrund.