상세 컨텐츠

본문 제목

[리눅스 명령어 정리] uniq

리눅스/linux 명령어 정리

by 해공학(해킹을 공부하는 학생) 2023. 5. 8. 21:39

본문

uniq명령어는 연속적으로 반복되는 행을 한행으로 줄여주는 명령어 이다.

uniq 는 sort명령어와 대부분 같이 쓰입니다.

그 이유는 sort명령어는 정렬을 해주기 때문에 한행이 몇번 반복되는지 한번에 알수있기 때문입니다. 

두 명령어를 이어 줄려면 파이프(|) 도 써야합니다.

사용법은 다음과 같습니다.

uniq [옵션] [파일 이름]

uniq를 아무 옵션 없이 사용해 보겠습니다

#원래 파일
┌──(root㉿kali)-[~/blog/pipe/uniq_sort]
└─# cat fruits_test              
apple
banana
lemon
Apple
apple
banana
banana
melon
melon
                                                                                                                             
#uniq를 쓴 파일
┌──(root㉿kali)-[~/blog/pipe/uniq_sort]
└─# cat fruits_test | sort | uniq 
apple
Apple
banana
lemon
melon

이런식으로 sort를 함깨 사용하면 정렬이 된후 중복되는게 없어지니 깔끔하게 할수있습니다.

 

옵션들 --- options

자주 사용하는 옵션들을 예제와 알아보겠습니다.

-c

연속적으로 반복된 수만큼 행 앞에 표시된다.

┌──(root㉿kali)-[~/blog/pipe/uniq_sort]
└─# cat fruits_test | sort | uniq -c
      2 apple
      1 Apple
      3 banana
      1 lemon
      2 melon

-u

연속적으로 반복되지 않은 행만 출력한다.

┌──(root㉿kali)-[~/blog/pipe/uniq_sort]
└─# cat fruits_test | sort | uniq -u
Apple
lemon

 

-d

u와 반대로, 반복된 행들만 한번 출력한다.

┌──(root㉿kali)-[~/blog/pipe/uniq_sort]
└─# cat fruits_test | sort | uniq -d
apple
banana
melon

 

-i

대소문자를 구별하지 않고 정렬한다.

┌──(root㉿kali)-[~/blog/pipe/uniq_sort]
└─# cat fruits_test | sort | uniq -i
apple
banana
lemon
melon

 

정리

uniq는 연속적으로 반복되는 행을 한행으로 만드는 명령어 이다.

sort와 대부분 같이 쓰이며, 파이프도 필수이다.

'리눅스 > linux 명령어 정리' 카테고리의 다른 글

[리눅스 명령어 정리] gizp, bzip2, tar  (0) 2023.05.12
[리눅스 명령어 정리] strings  (0) 2023.05.09
[리눅스 명령어 정리] sort  (0) 2023.05.07
[리눅스 명령어 정리]grep  (0) 2023.04.29
리다이렉션  (0) 2023.04.26

관련글 더보기