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

grep 심화

~15 min · grep, regex, search

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

모두가 처음 잡는 텍스트 검색 도구

grep = 'globally search a regular expression and print'. 패턴 매칭 줄을 출력. 기본 basic regex (BRE) 문법은 어색해 — 거의 모두 -E (extended) 또는 -P (Perl) 추가.

닳도록 쓰는 flag

  • -i — 대소문자 무시.
  • -r / -R — 재귀.
  • -n — 줄 번호.
  • -v — invert. 매치 안 되는 줄.
  • -l — 매치 있는 파일 이름만.
  • -c — 매치 개수.
  • -A 3 — 매치 뒤 3 줄 context. -B 3 앞, -C 3 양쪽.
  • -E — extended regex (alternation, +, ?, {n,m} 백슬래시 없이).
  • --include='*.py' / --exclude — 파일 필터.
  • --color=auto — 매치 강조.

흔한 패턴

  • 재귀 + 대소문자 무시: grep -rin 'TODO' .
  • 매치된 파일 이름만: grep -rl 'pattern' src/
  • 바이너리 건너뛰기: grep -rn --binary-files=without-match 'pattern' .
  • 파일별 개수: grep -rc 'TODO' . 가 file:count.
  • extended regex: grep -E 'foo|bar' file

Exit code

0 = 매치 있음, 1 = 없음, 2 = 에러. 그래서 grep -q 'pattern' file 이 스크립트의 완벽한 존재 검사: if grep -q ERROR build.log; then ...; fi.

grep 이 안 맞는 경우

여러 줄 패턴, 구조화 데이터 (JSON / YAML), 심볼 인식 검색은 modern-tools 트랙의 ripgrep (rg), jq, ast-grep.

Code

실전 grep 레시피·bash
# All TODO comments in Python files, with line numbers
grep -rn --include='*.py' 'TODO' .
# Show 2 lines of context around errors
grep -C 2 -i error build.log
# Existence check in a script
if grep -q '^export EDITOR=' ~/.zshrc; then
  echo 'EDITOR already set'
fi

External links

Exercise

프로젝트의 모든 TODO: grep -rn TODO .. 필터 추가: --include='*.py' --exclude-dir=node_modules. context 보기: grep -rn -C 2 'def main' .. 마지막으로 존재 검사 패턴을 작은 스크립트에 적용.

Progress

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

댓글 0

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

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