728x90
ubuntu@civic-asp:~/colors$ vi colors.txt
ubuntu@civic-asp:~/colors$ cat colors.txt
blue
black
red
red
green
blue
green
red
red
blue
colors.txt를 생성한다.
ubuntu@civic-asp:~/colors$ < colors.txt sort | uniq -c | sort -r | head -3 > favcolors.txt
ubuntu@civic-asp:~/colors$ la
colors.txt favcolors.txt
ubuntu@civic-asp:~/colors$ cat favcolors.txt
4 red
3 blue
2 green
colors.txt에서 가장 많이 존재하는 색상의 개수와 이름을 상위 3개 추출하는 명령어를 수행한다.
단계적으로 파악하자.
1. colors.txt 값을 입력으로 sort 명령어를 적용한 값을 step1.txt애 저장한다.
- sort
- 설명: 데이터를 정렬하는 명령어입니다.
- 기본 동작: 알파벳순으로 오름차순 정렬합니다.
- 옵션 사용 가능: -r (내림차순), -n (숫자 정렬), -u (중복 제거 후 정렬) 등.
ubuntu@civic-asp:~/colors$ < colors.txt sort > step1.txt
ubuntu@civic-asp:~/colors$ cat step1.txt
black
blue
blue
blue
green
green
red
red
red
red
2.step1.txt의 값을 입력으로 하여 uniq함수를 적용한 값을 step2.txt에 저장한다.
ubuntu@civic-asp:~/colors$ < step1.txt uniq -c > step2.txt
ubuntu@civic-asp:~/colors$ cat step2.txt
1 black
3 blue
2 green
4 red
- uniq -c
- 설명: 연속된 중복 라인을 제거하며 각 라인의 중복 개수를 출력
- -c 옵션: 중복된 라인의 개수를 해당 라인 앞에 표시합니다.
- 조건: uniq는 연속된 중복만 감지하므로, 중복을 정확히 처리하려면 반드시 sort로 데이터를 정렬해야 한다.
3.step2.txt의 값을 입력으로 하여 sort함수를 적용한 값을 step3.txt에 저장한다.
ubuntu@civic-asp:~/colors$ < step2.txt sort -r > step3.txt
ubuntu@civic-asp:~/colors$ cat step3.txt
4 red
3 blue
2 green
1 black
- 내림차순으로 정렬한다.
- 처음 입력 값인 colors.txt에서 가장 많이 존재한 색상을 파악하기위함
4. step3.txt를 입력값으로 하여 상위 3개의 값을 last.txt에 저장한다.
ubuntu@civic-asp:~/colors$ < step3.txt head -3 > last.txt
ubuntu@civic-asp:~/colors$ cat last.txt
4 red
3 blue
2 green
728x90
'😒 저 저 저 개념없는 나 > 🍎 Linux' 카테고리의 다른 글
[리눅스 | Linux] 리다이렉션 파이프라인 (0) | 2024.11.25 |
---|---|
[리눅스 | Linux] 리눅스 패키지 | 디렉토리 구조 | 마운트 (0) | 2024.11.25 |
[Linux | c] fgets 함수 (0) | 2022.04.15 |
[Linux | C] memset 함수 (0) | 2022.04.15 |
[Linux] System call 작동 원리, 동작 예시 (0) | 2022.03.23 |