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

tr — 글자 변환

~8 min · tr, character

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

가장 가벼운 텍스트 도구

tr 은 줄이나 단어가 아니라 개별 글자 단위로 작동. stdin 읽고 글자 변환 / 삭제 후 stdout. regex 없음, 필드 없음. 그냥 글자 매핑.

네 가지 유용한 모드

  • tr 'a-z' 'A-Z' — set 1 → set 2 (1:1).
  • tr -d 'aeiou' — 모든 모음 삭제.
  • tr -s '\n' — 연속 줄바꿈을 하나로.
  • tr -c 'a-zA-Z' '_' — complement: 글자 아닌 거 다 underscore.

특수 character class

[:upper:], [:lower:], [:digit:], [:space:], [:punct:] 인식. tr -d '[:punct:]' 가 구두점 제거, tr '[:upper:]' '[:lower:]' 가 locale 인식 소문자.

tr 이 못 하는 것

문자열 치환 X — 글자만. tr 'foo' 'bar' 는 'foo' → 'bar' 가 아니라 글자별 매핑 (f→b, o→a, o→r). 문자열 치환은 sed.

일상 활용

  • Windows 줄바꿈 제거: tr -d '\r' < in.txt > out.txt
  • 탭 → 공백: tr '\t' ' '
  • 대문자: echo 'hello' | tr a-z A-Z
  • 공백 압축: tr -s '[:space:]' ' '

Code

흔한 one-liner·bash
# Word-frequency the lazy way (lowercase + split into words)
tr 'A-Z' 'a-z' < article.txt \
  | tr -cs 'a-z' '\n'        \  # words on their own lines
  | sort | uniq -c | sort -rn | head

External links

Exercise

CR 제거: tr -d '\r' < windows.txt > unix.txt. 텍스트 소문자: cat ~/.zshrc | tr A-Z a-z | head. README 의 빈도: tr A-Z a-z < README.md | tr -cs a-z '\n' | sort | uniq -c | sort -rn | head.

Progress

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

댓글 0

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

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