camonitor_sort.pl: Support inverse regexp matches.
Annotate for file bin/camonitor_sort.pl
2006-09-22 pfeiffer 1 eval 'exec perl -S $0 ${1+"$@"}' # -*- Mode: perl -*-
12:46:44 ' 2 if 0;
' 3 # the above is a more portable way to find perl
' 4 # ! /usr/bin/perl
' 5
2022-01-24 Goetz 6 # Copyright 2022 Helmholtz-Zentrum Berlin für Materialien und Energie GmbH
2015-07-09 Goetz.Pfeiff 7 # <https://www.helmholtz-berlin.de>
09:34:31 ' 8 #
' 9 # Author: Goetz Pfeiffer <Goetz.Pfeiffer@helmholtz-berlin.de>
' 10 #
' 11 # This program is free software: you can redistribute it and/or modify it under
' 12 # the terms of the GNU General Public License as published by the Free Software
' 13 # Foundation, either version 3 of the License, or (at your option) any later
' 14 # version.
' 15 #
' 16 # This program is distributed in the hope that it will be useful, but WITHOUT
' 17 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
' 18 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
' 19 # details.
' 20 #
' 21 # You should have received a copy of the GNU General Public License along with
' 22 # this program. If not, see <http://www.gnu.org/licenses/>.
2007-10-23 pfeiffer 23
09:52:41 ' 24
2006-09-22 pfeiffer 25 # @STATUS: release
12:46:44 ' 26 # @PLATFORM: home bessy
' 27 # @CATEGORY: search
' 28
' 29
' 30 # [scriptname] -- describe the function here
' 31
' 32 use strict;
' 33
' 34 use FindBin;
' 35 use Getopt::Long;
2006-09-22 pfeiffer 36 use IO::Handle;
2006-09-22 pfeiffer 37
12:46:44 ' 38 use vars qw($opt_help $opt_summary
2006-09-22 pfeiffer 39 $opt_file $opt_name
2006-09-22 pfeiffer 40 $opt_time $opt_val
2006-11-23 pfeiffer 41 $opt_regexp $opt_progress
15:17:41 ' 42 $opt_rm_tmstamp
' 43 );
2006-09-22 pfeiffer 44
12:46:44 ' 45
2006-11-23 pfeiffer 46 my $sc_version= "1.0";
2006-09-22 pfeiffer 47
12:46:44 ' 48 my $sc_name= $FindBin::Script;
' 49 my $sc_summary= "sorts a log-file created by camonitor by timestamp";
' 50 my $sc_author= "Goetz Pfeiffer";
' 51 my $sc_year= "2005";
' 52
' 53 my $debug= 0; # global debug-switch
' 54
' 55
' 56 #Getopt::Long::config(qw(no_ignore_case));
' 57
' 58 if (!GetOptions("help|h","summary", "file|f=s",
2006-09-22 pfeiffer 59 "name|n=s", "time|t=s", "val|v=s",
2006-11-23 pfeiffer 60 "regexp|r=s", "progress|p", "rm_tmstamp|rm-tmstamp",
2006-09-22 pfeiffer 61 ))
12:46:44 ' 62 { die "parameter error!\n"; };
' 63
' 64 if ($opt_help)
' 65 { help();
' 66 exit;
' 67 };
' 68
' 69 if ($opt_summary)
' 70 { print_summary();
' 71 exit;
' 72 };
2007-10-23 pfeiffer 73
2006-09-22 pfeiffer 74 STDERR->autoflush(1);
2006-09-22 pfeiffer 75
2006-09-22 pfeiffer 76 mk_regexp("n_regexp",$opt_name);
2006-09-22 pfeiffer 77 mk_regexp("t_regexp",$opt_time);
12:46:44 ' 78 mk_regexp("v_regexp",$opt_val);
2006-09-22 pfeiffer 79 mk_regexp("r_regexp",$opt_regexp);
2006-09-22 pfeiffer 80
12:46:44 ' 81 my $r_lines= slurp($opt_file);
2006-09-22 pfeiffer 82 my $r_h= mk_hash($r_lines,
15:10:23 ' 83 $opt_name,$opt_time,$opt_val,$opt_regexp);
2006-09-22 pfeiffer 84 print_sorted($r_h);
12:46:44 ' 85 exit(0);
' 86
' 87 # ------------------------------------------------
' 88
' 89 #die "no command-line options given!";
' 90
' 91 # fit in program text here
' 92
' 93 # ------------------------------------------------
' 94
' 95 sub mk_regexp
' 96 { my($funcname,$regexp)= @_;
2022-03-25 Goetz.Pfeiff 97 my $invert= 0;
2006-09-22 pfeiffer 98
12:46:44 ' 99 return if (!defined $regexp);
' 100
2022-03-25 Goetz.Pfeiff 101 if ($regexp =~ /^!(.*)/)
21:20:17 ' 102 {
' 103 $regexp= $1;
' 104 $invert= 1;
' 105 }
' 106
2006-09-22 pfeiffer 107 if ($regexp !~ /^\//)
12:46:44 ' 108 { $regexp= '/' . $regexp . '/'; };
' 109
' 110 #die "eval:\"sub n_regexp { return(\$_[0]=~ $regexp); }\"";
2022-03-25 Goetz.Pfeiff 111 if (!$invert)
21:20:17 ' 112 {
' 113 eval("sub $funcname { return(\$_[0]=~ $regexp); }");
' 114 }
' 115 else
' 116 {
' 117 eval("sub $funcname { return(\$_[0]!~ $regexp); }");
' 118 }
2006-09-22 pfeiffer 119 if ($@)
12:46:44 ' 120 { die "error: eval() failed, error-message:\n" . $@ . " " };
' 121 }
' 122
' 123 sub slurp
' 124 { my($file)= @_;
' 125 # local $/;
' 126 # undef $/;
' 127 local(*F);
' 128 my @lines;
2006-09-22 pfeiffer 129 my $cnt=100;
2007-10-23 pfeiffer 130
2006-09-22 pfeiffer 131 if (defined $file)
12:46:44 ' 132 { open(F,$file) || die "unable to open $file"; }
' 133 else
' 134 { *F= *STDIN; };
2006-09-22 pfeiffer 135
2006-09-22 pfeiffer 136 while(my $st=<F>)
2006-09-22 pfeiffer 137 { if ($opt_progress)
15:10:23 ' 138 { if (--$cnt==0)
' 139 { print STDERR '.';
' 140 $cnt= 100;
' 141 };
' 142 };
' 143 chomp($st);
2006-09-22 pfeiffer 144 push @lines,$st;
12:46:44 ' 145 };
' 146
' 147 if (defined $file)
' 148 { close(F); };
2007-10-23 pfeiffer 149
2006-09-22 pfeiffer 150 print STDERR "\n" if ($opt_progress);
2007-10-23 pfeiffer 151
2006-09-22 pfeiffer 152 return(\@lines);
12:46:44 ' 153 }
' 154
' 155 sub mk_hash
2006-09-22 pfeiffer 156 { my($r_lines,$n_regexp,$t_regexp,$v_regexp,$r_regexp)= @_;
2006-09-22 pfeiffer 157 my %h;
2006-09-22 pfeiffer 158 my $cnt=100;
2018-12-11 Goetz.Pfeiff 159 my $lineno= 0;
2007-10-23 pfeiffer 160
2006-09-22 pfeiffer 161 for(my $i=0; $i<=$#$r_lines; $i++)
2018-12-11 Goetz.Pfeiff 162 {
12:09:10 ' 163 $lineno+= 1;
' 164 if ($opt_progress)
2006-09-22 pfeiffer 165 { if (--$cnt==0)
15:10:23 ' 166 { print STDERR ':';
' 167 $cnt= 100;
' 168 };
' 169 };
2006-09-22 pfeiffer 170 my $line= $r_lines->[$i];
12:46:44 ' 171 my @a= split(/\s+/,$line);
2007-10-23 pfeiffer 172
2006-09-22 pfeiffer 173 if (defined $n_regexp)
15:10:23 ' 174 { next if (!n_regexp($a[0])); };
2007-10-23 pfeiffer 175
2006-09-22 pfeiffer 176 next if ($a[1] eq '<undefined>');
2007-10-23 pfeiffer 177
2006-09-22 pfeiffer 178 if (defined $t_regexp)
12:46:44 ' 179 { next if (!t_regexp($a[1] . " " . $a[2])); };
' 180
' 181 if (defined $v_regexp)
' 182 { next if (!v_regexp($a[3])); };
2006-09-22 pfeiffer 183
2006-09-22 pfeiffer 184 if (defined $r_regexp)
2006-09-22 pfeiffer 185 { next if (!r_regexp($line)); };
2007-10-23 pfeiffer 186
2018-12-11 Goetz.Pfeiff 187 # we include a line number since it may be that a record is in the file
12:09:10 ' 188 # with the same timestamp but different values:
' 189 my $key= $a[1] . "," . $a[2] . "," . $a[0] . "," .
' 190 sprintf("%010d", $lineno);
2006-09-22 pfeiffer 191 #print "$key->",$line,"\n";
2007-10-23 pfeiffer 192
2006-11-23 pfeiffer 193 if ($opt_rm_tmstamp)
15:17:41 ' 194 { $line= sprintf "%-40s %s",$a[0],$a[3]; };
2007-10-23 pfeiffer 195
2006-09-22 pfeiffer 196 $h{$key}= $line;
12:46:44 ' 197 };
2006-09-22 pfeiffer 198 print STDERR "\n" if ($opt_progress);
2006-09-22 pfeiffer 199 return(\%h);
12:46:44 ' 200 }
2007-10-23 pfeiffer 201
2006-09-22 pfeiffer 202 sub print_sorted
12:46:44 ' 203 { my($r_h)= @_;
2007-10-23 pfeiffer 204
2006-09-22 pfeiffer 205 foreach my $k (sort keys %$r_h)
12:46:44 ' 206 { print $r_h->{$k},"\n"; };
' 207 }
2007-10-23 pfeiffer 208
2006-09-22 pfeiffer 209
12:46:44 ' 210 sub print_summary
' 211 { printf("%-20s: $sc_summary\n",
' 212 $sc_name);
' 213 }
' 214
' 215 sub h_center
' 216 { my($st)= @_;
' 217 return( (' ' x (38 - length($st)/2)) . $st );
' 218 }
' 219
' 220 sub help
' 221 { my $l1= h_center("**** $sc_name $sc_version -- $sc_summary ****");
' 222 my $l2= h_center("$sc_author $sc_year");
' 223 print <<END;
' 224
' 225 $l1
' 226 $l2
' 227
' 228 Syntax:
' 229 $sc_name {options}
' 230
2022-03-25 Goetz.Pfeiff 231 Note: every regexp that starts with '!' is a "do not match" regexp. In this
21:20:17 ' 232 case theresults are the ones that DO NOT match.
' 233
2006-09-22 pfeiffer 234 options:
12:46:44 ' 235 -h: help
' 236 --summary: give a summary of the script
' 237 -f [file]: read that file, otherwise read STDIN
2006-09-22 pfeiffer 238 -n [regexp]: filter record names (records must match this)
2006-09-22 pfeiffer 239 -t [regexp]: filter times (times must match this)
12:46:44 ' 240 examples:
' 241 -t 2006-09-14
' 242 print only from that date
' 243 -t '2006-09-14 23'
' 244 print only from 2006-09-14, 23:00 to 23:59
' 245 -t '2006-09-14 (22|23)'
' 246 print only from 2006-09-14, 22:00 to 23:59
' 247 -v [regexp]: filter values (values must match this)
' 248 examples
' 249 -v '\d+'
' 250 print only records where the value is an integer
' 251 -v '(enabled|disabled)'
2006-09-22 pfeiffer 252 print only records where the value is "enabled" or "disabled"
2006-09-22 pfeiffer 253 -r [regexp] print only lines where the LINE matches the regexp
15:10:23 ' 254 -p show progress on STDERR
2006-11-23 pfeiffer 255 --rm-tmstamp remove timestamps in output
2006-09-22 pfeiffer 256 END
12:46:44 ' 257 }
' 258