![출력별로 정렬된 여러 파일을 Grep](https://linux55.com/image/132226/%EC%B6%9C%EB%A0%A5%EB%B3%84%EB%A1%9C%20%EC%A0%95%EB%A0%AC%EB%90%9C%20%EC%97%AC%EB%9F%AC%20%ED%8C%8C%EC%9D%BC%EC%9D%84%20Grep.png)
grep을 사용하여 여러 로그 파일을 검색하고 있습니다. 로그에서 각 줄의 첫 번째 부분은 hh:mm:ss 형식의 시간입니다. 이번에는 정렬 기준이 되어야 합니다.
정렬을 사용하면 grep -r string1 | grep --color string2 | sort
파일 이름에 적용됩니다(유용한 정보임). 매개변수를 추가하면 -n
포함된 줄 번호가 사용되기 때문에 상황이 더 악화됩니다.
로그 형식은 다음과 같습니다.
354058:c1.com/client-01.log:13:30:04 INFO: Item: created widget in ARRIVALS
360746:c1.com/client-01.log:13:39:46 INFO: change transfer to contact
379144:c3.com/client-01.log:13:30:02 INFO: Item: created widget in ARR
392162:c3.com/client-01.log:13:51:59 INFO: Item: created widget in ARR
367422:c5.com/client-01.log:13:51:56 INFO: searching for Sign
367446:c5.com/client-01.log:13:51:59 INFO: Item: created widget in ARRIVALS
367629:c5.com/client-01.log:13:52:12 INFO: change from initial to contact
371979:c9.com/client-01.log:14:00:19 INFO: change workflow to tgl
편집: 내 예상 유형은 다음과 같습니다.
379144:c3.com/client-01.log:13:30:02 INFO: Item: created widget in ARR
354058:c1.com/client-01.log:13:30:04 INFO: Item: created widget in ARRIVALS
360746:c1.com/client-01.log:13:39:46 INFO: change transfer to contact
367422:c5.com/client-01.log:13:51:56 INFO: searching for Sign
392162:c3.com/client-01.log:13:51:59 INFO: Item: created widget in ARR
367446:c5.com/client-01.log:13:51:59 INFO: Item: created widget in ARRIVALS
367629:c5.com/client-01.log:13:52:12 INFO: change from initial to contact
371979:c9.com/client-01.log:14:00:19 INFO: change workflow to tgl
| sort -t'/' -k2V
대부분의 결과는 제안된 변경 사항 및 추가 사항을 기반으로 합리적으로 보이지만 다른 출력의 대부분은 완벽하게 괜찮은데 갑자기 이런 결과가 나타납니다.
353619:d1.com/core-01.log:14:09:45 INFO: Server: updating
33:c5.com/client-01.log:13:30:02 INFO: Item: created widget in ACTIVE
답변1
주어진 파일
354058:c1.com/client-01.log:13:30:04 INFO: Item: created widget in ARRIVALS
360746:c1.com/client-01.log:13:39:46 INFO: change transfer to contact
379144:c3.com/client-01.log:13:30:02 INFO: Item: created widget in ARR
392162:c3.com/client-01.log:13:51:59 INFO: Item: created widget in ARR
353619:d1.com/core-01.log:14:09:45 INFO: Server: updating
367422:c5.com/client-01.log:13:51:56 INFO: searching for Sign
367446:c5.com/client-01.log:13:51:59 INFO: Item: created widget in ARRIVALS
367629:c5.com/client-01.log:13:52:12 INFO: change from initial to contact
371979:c9.com/client-01.log:14:00:19 INFO: change workflow to tgl
주문하다
sort -t: -k3 file
:
사전 정렬은 세 번째 구분 필드(및 그 뒤의 모든 필드)를 정렬 키로 사용하여 수행됩니다. 결과는 다음과 같습니다
379144:c3.com/client-01.log:13:30:02 INFO: Item: created widget in ARR
354058:c1.com/client-01.log:13:30:04 INFO: Item: created widget in ARRIVALS
360746:c1.com/client-01.log:13:39:46 INFO: change transfer to contact
367422:c5.com/client-01.log:13:51:56 INFO: searching for Sign
392162:c3.com/client-01.log:13:51:59 INFO: Item: created widget in ARR
367446:c5.com/client-01.log:13:51:59 INFO: Item: created widget in ARRIVALS
367629:c5.com/client-01.log:13:52:12 INFO: change from initial to contact
371979:c9.com/client-01.log:14:00:19 INFO: change workflow to tgl
353619:d1.com/core-01.log:14:09:45 INFO: Server: updating
이렇게 하려면 필드 3, 4, 5의 숫자가 항상 0으로 채워져 있다고 가정합니다.