Thread Zeilen löschen aus Array, wenn bestimmte Strings enthalten (17 answers)
Opened by cohama at 2012-05-14 13:40

cohama
 2012-05-14 15:14
#158344 #158344
User since
2011-08-16
102 Artikel
BenutzerIn

user image
Hallo zusammen,
vielen Dank für die Hinweise bis jetzt. In der Zwischenzeit habe ich mein Script etwas weiter entwickelt. Und zwar durchsuche ich die Syslog-Datei zu erst auf Schlüsselwörter wie user.err, user.crit oder user.alert.
In dem zweiten Schritt möchte ich gern Zeile nicht weiter behandeln, wenn sie Schlüsselwörter wie WLAN enthalten. Somit ist es dann ein zweistufiges Suchen.
Mein Skript bis jetzt.

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
#!/usr/bin/perl -w
########################################
# Modules
use strict; # parameter Definition before use them
####################################
# modules
#######################################
# Paramet Definition
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$ydat,$isdst);
my ($jahr,$monat,$tag,$Xzeit,$Xdatum);
my $ct=1; # counter parameter
my $lines; # line parameter
my @data1 =("");
my $kw1="user.alert";
my $kw2="user.crit";
my $kw3="user.err";
my $ukw1="WLAN";

##################
# main programm
system("clear"); # Refresh the terminal
####################
# Time procedure
($sec,$min,$hour,$mday,$mon,$year,$wday,$ydat,$isdst)=localtime(); # Get TimeStamp-Elements
$jahr=$year;$monat=$mon+1;$tag=$mday;$jahr=$year;

$jahr=$year +1900; # Set the Year
if (length($monat) == 1){$monat="0$monat";} # Fillup the number amount
if(length($tag) == 1){ $tag="0$tag";}
if(length($hour) == 1){ $hour="0$hour";}
if(length($min) == 1){ $min="0$min";}
if(length($sec) == 1){ $sec="0$sec";}
#$Xdatum=$tag.".".$monat.".".$jahr;
$Xdatum=$tag.".".$monat;
$Xzeit=$hour.":".$min;
#########################
# Logfile analysis
open(InFile,$ARGV[0])or die $!; # read Inputfile
open(OutFile,">/home/cohama/SysLog/$Xdatum._SysLog_PTA-SIP.log")or die $!;
while ($lines = <InFile>) {
switch($lines){
case 1: =~ m{$kw1} {push(@data1,$lines);break; # read File line per line
case 2: =~ m{$kw2} {push(@data1,$lines);break;
case 3: =~ m{$kw3} {push(@data1,$lines);break;
}
#################################################
}
print "Store2File \n";
print OutFile @data1;
print "Close the files \n";
close(InFile);close(OutFile); # write Array 2 File

View full thread Zeilen löschen aus Array, wenn bestimmte Strings enthalten