로그 파일을 다듬는 Linux 명령

로그 파일을 다듬는 Linux 명령

version.txt라는 로그 파일이 있고 키워드가 있는 모든 단어를 필터링해야 합니다 (time=. 출력은 (time=451)다음과 유사해야 합니다.

다음은 로그 파일의 샘플 항목입니다. 내가 나열하고 싶은 항목을 강조 표시합니다. 잘라내거나 자르면 단어 대신 전체 줄이 인쇄됩니다 (time=.

(time=숫자를 포함하는 단어부터 끝까지 단어를 나열하는 명령을 제안해 주세요 ).

2020-03-10 06:48:20 [http-nio-7001-exec-7] INFO  [5e6770737be8a35b5fef38f7be2a2635] [5fef38f7be2a2635] [] c.l.e.i.a.c.ItemAvailabilityControllerImpl - DeliveryMethod(sosItmNbr=null, fullMtdTyp=3, fullMtdMsg=Delivery, fullCarrier=null, fullCarrierSvc=null, fullTransitMode=null, fullLctNbr=0, restMsg=null, isAvlSts=false, reqStates=[], onhandQty=0, totalQty=0, itmLdTmAvlQty=0, itmLdTm=null, itmConsolidationDate=null, itmLdTmDays=null, itmLdTmDaysLow=null, fullPath=null)])]) (time=451) 
2020-03-10 06:48:20 [http-nio-7001-exec-28] INFO  [5e677073e64bd99b5997b5bd20c3c4e0] [5997b5bd20c3c4e0] [] c.l.e.i.a.c.ItemAvailabilityControllerImpl - Finished availability process; Response: IAResponse(locationItemData=[ResponseItem(lctNbr=6877, itemNbr=10000070, modID=1500040, omniID=null, vbuNbr=14692, itmTypCode=3, reqQty=17, itemAvailList=[DeliveryMethod(sosItmNbr=null, fullMtdTyp=1, fullMtdMsg=Parcel, fullCarrier=null, fullCarrierSvc=null, fullTransitMode=null, fullLctNbr=0, restMsg=null, isAvlSts=false, reqStates=[], onhandQty=0, totalQty=0, itmLdTmAvlQty=0, itmLdTm=null, itmConsolidationDate=null,(time=455)
2020-03-10 06:48:20 [http-nio-7001-exec-46] INFO  [5e6770731c4e323f4cb875712bb0d8ee] [4cb875712bb0d8ee] [] c.l.e.i.a.c.ItemAvailabilityControllerImpl - Finised (time=492)

이 예제 입력의 출력은 다음과 같아야 합니다.

(time=451)
(time=455)
(time=492)

답변1

문제가 완전히 명확하지 않습니다.

주문하다

grep -o '(time=[[:digit:]]*)' inputfile

문제 인쇄의 샘플 입력

(time=451)
(time=455)
(time=492)

의견에서 추가 요구 사항을 다루도록 편집되었습니다.

줄 시작 부분의 출력에 날짜 및 시간 필드를 추가하려면 다른 명령이 필요합니다.

입력의 모든 줄에 (time=...)날짜 및 시간 필드가 포함되어 있고 단일 공백 ​​문자로 구분된 날짜 및 시간 필드로 시작하는 경우 다음을 사용할 수 있습니다.

sed 's/^\([-0-9]* [:0-9]* \).*\((time=[[:digit:]]*)\).*/\1\2/' inputfile

이 인쇄

2020-03-10 06:48:20 (time=451)
2020-03-10 06:48:20 (time=455)
2020-03-10 06:48:20 (time=492)

일치하지 않는 다른 행이 있는 경우 grep위와 동일한 패턴으로 결합할 수 있지만 생략할 수 있습니다 -o.

grep '(time=[[:digit:]]*)' inputfile | sed 's/^\([-0-9]* [:0-9]* \).*\((time=[[:digit:]]*)\).*/\1\2/'

내 명령의 검색 패턴은 sed그다지 엄격하지 않습니다.

이 부분은 [-0-9]* [:0-9]*임의의 숫자와 대시(날짜), 공백, 임의의 숫자와 콜론(시간), 공백의 조합과 일치합니다. 패턴은 이전 패턴을 통해 줄의 시작 부분에 고정됩니다 ^.

---123--456- 9876:54321따라서 줄 시작 부분의 공백 두 개와 같이 잘못된 날짜 및 시간 필드와도 일치합니다 .

관련 정보