Skip to content

Commit

Permalink
Create 리눅스 - 로그 파일에서 특정 내용의 유일값 구하기.md
Browse files Browse the repository at this point in the history
  • Loading branch information
HomoEfficio committed Dec 21, 2016
1 parent dde1f87 commit b27e468
Showing 1 changed file with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# 리눅스 - 로그 파일에서 특정 내용의 유일값 구하기

아래와 같은 로그 파일 `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
```

----
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/"><img alt="크리에이티브 커먼즈 라이선스" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png" /></a>

<a href='https://www.facebook.com/hanmomhanda' target='_blank'>HomoEfficio</a>가 작성한 이 저작물은

<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">크리에이티브 커먼즈 저작자표시-비영리-동일조건변경허락 4.0 국제 라이선스</a>에 따라 이용할 수 있습니다.

0 comments on commit b27e468

Please sign in to comment.