파일에서 패턴 발생 추출

파일에서 패턴 발생 추출

아래 표시된 것과 유사한 로그가 포함된 대용량 파일이 있습니다. 이 오류의 영향을 받은 모든 거래(TR#)를 찾고 싶습니다. 각 TR# ID를 하나씩 추출해야 합니다.

어떻게 해야 하나요?

    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

원하는 출력:

    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

답변1

이것은 매우 간단합니다 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

또는 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

위의 내용에서는 앞의 각 숫자가 TR#IDID의 일부라고 가정합니다. 숫자를 변경할 수 있지만 그 중 하나만 필요한 경우 다음을 대신 사용하세요.

$ awk -F'[:.]' 'c[$7]++==1' file 

또는

$ perl -F'[:.]' -ane '$k{$F[6]}++==1 && print' file 

답변2

각 메시지의 첫 번째 발생을 가져와 인쇄하려면 다음을 시도하십시오.

awk '! m[$5] {m[$5]=$0} END{for (e in m) print m[e]}'

테스트를 위해 예제의 타임스탬프를 연속적으로 설정했습니다(그리고 마지막으로 잘린 잘못된 값도 수정했습니다).

$ 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

감사합니다 @terdon

답변3

다음은 원하는 작업을 수행하는 Perl 스크립트입니다.

#!/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;
}

귀하의 입력으로 호출하면 다음과 같은 결과가 나옵니다.

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

답변4

GNU를 통해 sed,이것그러니 대답해,

sed '$!N; /^\(.*\)\n\1$/!P; D' file

관련 정보