C.W.K.
Stream
Lesson 09 of 10 · published

cut 과 tr

~10 min · cut, tr, fields

Level 0창 구경꾼
0 XP0/95 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

cut — 컬럼 슬라이스

cut 이 구조화 입력에서 필드 추출. 세 모드:

  • cut -f 2 — 두 번째 탭 구분 필드.
  • cut -d ',' -f 1,3 — 쉼표 구분 1, 3 번째.
  • cut -c 1-10 — 각 줄의 첫 10 글자.
  • cut -d ' ' -f 2- — 2 번째부터 끝까지.

CSV, TSV, 고정 구분자 로그에 좋아. 복잡한 파싱은 awk (다음 트랙).

tr — 문자 변환 / 삭제

tr 가 stdin 을 글자 단위로 읽어서 변환. 주요 형:

  • tr 'a-z' 'A-Z' — 대문자 변환.
  • tr -d ' ' — 공백 삭제.
  • tr -s ' ' — 연속 공백을 하나로.
  • tr -c 'a-zA-Z0-9' '_' — 영숫자가 아닌 거 다 _ 으로 (complement).

현실 콤보

  • 로그의 unique IP: cut -d ' ' -f 1 access.log | sort -u
  • 파일을 소문자로: tr 'A-Z' 'a-z' < INPUT > output
  • Windows 줄바꿈 제거: tr -d '\r' < in.txt > out.txt
  • 탭을 공백으로: tr '\t' ' '

cut 의 한계

cut 은 따옴표 안 쉼표 (quoted CSV) 처리 못 해. 진짜 CSV 는 csvkit, miller, Python csv 모듈로. 단순 구분 로그엔 cut 충분.

Code

/etc/passwd 에서 사용자 추출·bash
cut -d ':' -f 1 /etc/passwd | head
# With shell
cut -d ':' -f 1,7 /etc/passwd | head
이름 필드 정규화·bash
echo '  John   Doe' \
  | tr -s ' '            \  # collapse whitespace
  | tr 'A-Z' 'a-z'       \  # lowercase
  | sed 's/^ //'         \  # leading space
  | tr ' ' '-'              # space -> dash

External links

Exercise

cut -d ':' -f 1 /etc/passwd | sort 으로 사용자 계정 정렬. echo 'Hello World' | tr 'a-z' 'A-Z'. 마지막으로 cat ~/.zshrc | tr -s ' ' | head 로 squeeze 효과 보기.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.