Ich habe eine große Datei mit Protokollen, die denen unten ähnlich sind. Ich möchte alle Transaktionen (TR#) finden, die von dem Fehler betroffen waren. Ich muss ein Vorkommen jeder TR#-ID extrahieren.
Wie könnte ich vorgehen?
Apr 30 16:51:29.574 application.crit: [6104]:TR#14. Transaction send can not be sent. Error Code: 704
Apr 30 16:51:29.574 application.crit: [6104]:TR#14. Transaction send can not be sent. Error Code: 704
Apr 30 16:51:29.574 application.crit: [6104]:TR#14. Transaction send can not be sent. Error Code: 704
Apr 30 16:51:29.574 application.crit: [6104]:TR#14. Transaction send can not be sent. Error Code: 704
Apr 30 16:51:29.574 application.crit: [6104]:TR#238. Transaction send can not be sent. Error Code: 704
Apr 30 16:51:29.574 application.crit: [6104]:TR#238. Transaction send can not be sent. Error Code: 704
Apr 30 16:51:29.574 application.crit: [6104]:TR#238. Transaction send can not be sent. Error Code: 704
Apr 30 16:51:29.574 application.crit: [6104]:TR#238. Transaction send can not be sent. Error Code: 704
Apr 30 16:51:29.574 application.crit: [6104]:TR#238. Transaction send can not be sent. Error Code: 704
Apr 30 16:51:29.574 application.crit: [6104]:TR#238. Transaction send can not be sent. Error Code: 704
Erforderliche Ausgabe:
Apr 30 16:51:29.574 application.crit: [6104]:TR#14. Transaction send can not be sent. Error Code: 704
Apr 30 16:51:29.574 application.crit: [6104]:TR#238. Transaction send can not be sent. Error Code: 704
Antwort1
Dies ist ganz einfach in awk
:
$ awk 'c[$5]++==1' file
Apr 30 16:51:29.574 application.crit: [6104]:TR#14. Transaction send can not be sent. Error Code: 704
Apr 30 16:51:29.574 application.crit: [6104]:TR#238. Transaction send can not be sent. Error Code: 704
Oder in Perl:
$ perl -ane '$k{$F[4]}++==1 && print' file
Apr 30 16:51:29.574 application.crit: [6104]:TR#14. Transaction send can not be sent. Error Code: 704
Apr 30 16:51:29.574 application.crit: [6104]:TR#238. Transaction send can not be sent. Error Code: 704
Oben wird davon ausgegangen, dass die Nummer vor jeder Nummer TR#ID
Teil der ID ist. Wenn sich die Nummern ändern können, Sie aber nur eine davon benötigen, verwenden Sie stattdessen Folgendes:
$ awk -F'[:.]' 'c[$7]++==1' file
oder
$ perl -F'[:.]' -ane '$k{$F[6]}++==1 && print' file
Antwort2
Um das erste Vorkommen jeder Nachricht abzurufen und auszudrucken, versuchen Sie
awk '! m[$5] {m[$5]=$0} END{for (e in m) print m[e]}'
Ich habe die Zeitstempel in Ihrem Beispiel sequenziell gemacht, um es zu testen (und auch den endgültigen abgeschnittenen Fehlerwert korrigiert):
$ awk '! m[$5] {m[$5]=$0} END{for (e in m) print m[e]}' tr2.log
Apr 30 16:51:27.574 application.crit: [6104]:TR#14. Transaction send can not be sent. Error Code: 704
Apr 30 16:51:31.574 application.crit: [6104]:TR#238. Transaction send can not be sent. Error Code: 704
Mit Dank an @terdon
Antwort3
Hier ist ein Perl-Skript, das macht, was Sie wollen:
#!/usr/bin/perl
#Read each line
while ($line = <>) {
# Extract the transaction ID by looking for the text TR followed by digits
($trid) = $line =~ /.*(TR#\d+).*/ ;
# If we've not seen the ID before, print it out
unless ($trids{$trid}) {
print $line;
}
# Remember the ID so we don't print it out again
$trids{$trid} = 1;
}
Wenn ich es mit Ihrer Eingabe aufrufe, erhalte ich Folgendes:
temeraire:ul jenny$ ./extract.pl in.txt
Apr 30 16:51:29.574 application.crit: [6104]:TR#14. Transaction send can not be sent. Error Code: 704
Apr 30 16:51:29.574 application.crit: [6104]:TR#238. Transaction send can not be sent. Error Code: 704
Antwort4
Durch GNU sed
, gestohlen vonDasAlso antworte,
sed '$!N; /^\(.*\)\n\1$/!P; D' file