Skip to content

Latest commit

 

History

History
95 lines (73 loc) · 3.24 KB

리눅스 - 로그 파일에서 특정 내용의 유일값 구하기.md

File metadata and controls

95 lines (73 loc) · 3.24 KB

리눅스 - 로그 파일에서 특정 내용의 유일값 구하기

아래와 같은 로그 파일 my.log 에서 맨 끝에 있는 svc-id 값의 유일한 값의 목록을 어떻게 구할 수 있을까?

21 Dec 02:24:07 - put_meta skipping OK at product.coffe 2 : svc-id 1020
21 Dec 02:24:07 - put_meta skipping OK at product.coffe 2 : svc-id 1020
21 Dec 02:24:07 - tag nike at notice.js 3 : svc-id 1188
21 Dec 02:24:07 - put_meta skipping OK at product.coffe 2 : svc-id 1647
21 Dec 02:24:08 - put_meta skipping OK at product.coffe 1 : svc-id 779
21 Dec 02:24:08 - put_meta skipping OK at product.coffe 2 : svc-id 312
21 Dec 02:24:08 - tag addidas at notice.js 3 : svc-id 978
21 Dec 02:24:08 - put_meta skipping OK at product.coffe 2 : svc-id 312
21 Dec 02:24:08 - put_meta skipping OK at product.coffe 2 : svc-id 779
21 Dec 02:24:08 - put_meta skipping OK at product.coffe 1 : svc-id 1811
21 Dec 02:24:08 - tag nike at notice.js 5 : svc-id 7388
21 Dec 02:24:08 - put_meta skipping OK at product.coffe 1 : svc-id 779
21 Dec 02:24:08 - put_meta skipping OK at product.coffe 1 : svc-id 806
21 Dec 02:24:08 - put_meta skipping OK at product.coffe 1 : svc-id 1767
21 Dec 02:24:09 - put_meta skipping OK at product.coffe 1 : svc-id 408
21 Dec 02:24:09 - put_meta skipping OK at product.coffe 1 : svc-id 1811
21 Dec 02:24:09 - put_meta skipping OK at product.coffe 2 : svc-id 1255
21 Dec 02:24:09 - put_meta skipping OK at product.coffe 1 : svc-id 241
21 Dec 02:24:09 - put_meta skipping OK at product.coffe 1 : svc-id 1673
21 Dec 02:24:09 - put_meta skipping OK at product.coffe 1 : svc-id 1673
21 Dec 02:24:09 - put_meta skipping OK at product/index.js 1 : svc-id 1673
21 Dec 02:24:09 - put_meta skipping OK at product/index.js 2 : svc-id 1673

유일값 목록 구하기

skipping을 포함하는 행의 svc-id 의 유일한 값의 목록을 구하려면

grep 'skipping' my.log | cut -f13 -d ' ' | sort | uniq

위 명령에서 cut -f13 -d ' '' '로 구분(delimit)해서 13번째 컬럼에 있는 값을 추출한다.

  • 결과

     1020
     1255
     1647
     1673
     1767
     1811
     241
     312
     408
     779
     806
    

결과가 나오기는 했는데 숫자인데도 불구하고 문자열 기준으로 정렬이 되어 있다.

유일값이 숫자일 경우 숫자 순서로 정렬하기

아래와 같이 sort-g 옵션을 주면 숫자를 숫자 기준으로 정렬할 수 있다.

grep 'skipping' my.log | cut -f13 -d ' ' | sort -g | uniq

  • 결과

     241
     312
     408
     779
     806
     1020
     1255
     1647
     1673
     1767
     1811
    

유일값 개수 구하기

skipping을 포함하는 행의 svc-id 의 유일한 값의 개수를 구하려면

grep 'skipping' my.log | cut -f13 -d ' ' | sort -g | uniq | wc -l

  • 결과

     11
    

크리에이티브 커먼즈 라이선스

HomoEfficio가 작성한 이 저작물은

크리에이티브 커먼즈 저작자표시-비영리-동일조건변경허락 4.0 국제 라이선스에 따라 이용할 수 있습니다.