Last active
August 29, 2015 14:04
-
-
Save leonardoruoso/e21eae8603653dabfd93 to your computer and use it in GitHub Desktop.
cascavel.pm - * Eu preciso que a ocorrência da string CRITICO, tenha preferência sobre o OK, tipo se eu tiver a ocorrência da string CRITICO, ele exiba as linhas com a ocorrência de critico e não exiba as linhas OK. * Caso não encontre a ocorrência da string CRITICO, ele exiba a ocorrência da string OK.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use autodie; | |
use common::sense; | |
=pod | |
=head1 Sobre | |
O resquisito é analisar múltiplos arquivos de log gerados simultaneamente, com as linhas sincronizadas, e imprimir uma linha com o reultado com o primeiro OK contido, exceto no caso de imprimir o primeiro resultado contendo CRITICO. Imprimir nada se nenhum dos dois dor encontrado. | |
=cut | |
my @arquivo = @ARGV; | |
$SIG{INT} = sub { close($arquivo[$_]) for (0..$#arquivo); exit(0); }; | |
die 'Ao menos dois arquivos são necessários para a comparação!' if $#ARGV <= 2; | |
main(); | |
sub main { | |
open($arquivo[$_], '<', $arquivo[$_]) for (0..$#arquivo); | |
TAIL: for (;;) { | |
# For labels not really needed | |
my $eof; | |
my @linha_arquivo; | |
$eof ||= eof($arquivo[$_]) for (0..$#arquivo); | |
if ($eof) { | |
sleep 5; | |
seek($arquivo[$_], 0, 1) for (0..$#arquivo); | |
next TAIL; | |
} | |
$linha_arquivo[$_] = readline $arquivo[$_] for (0..$#arquivo); | |
my $output; | |
CRITICO: for (0..$#arquivo) { | |
last CRITICO if defined($output); | |
$output = $linha_arquivo[$_] if $linha_arquivo[$_] =~ /CRITICO/; | |
} | |
OK: for (0..$#arquivo) { | |
last OK if defined($output); | |
$output = $linha_arquivo[$_] if $linha_arquivo[$_] =~ /OK/; | |
} | |
print STDOUT $output."\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment