Text CLI (grep / sed / awk) 치트시트
실제 파이프라인으로 배우는 Unix 텍스트 처리 명령어: GNU grep/ripgrep 검색, sed 스트림 편집, awk 필드 처리 및 집계, cut, sort, uniq, tr.
동작 원리
Unix text tools act like stream filters: each tool reads lines from files or stdin, does one small job (search, slice columns, replace, count, sort) and writes lines to stdout so you can snap them together with `|`.
서바이벌 키트
가장 먼저 배워야 할 몇 가지 명령어입니다.
41개 명령어
Search lines (grep)
grep prints lines matching a pattern. Add -n for line numbers, -i for case-insensitive and -E for extended regex.
공식 문서: grep usage- 기본
Search recursively and print file paths and line numbers.
grep -rn "<pattern>" <dir>명령어 만들기
grep -rn "<pattern>" <dir>값은 입력한 그대로 삽입됩니다. 공백이나 특수 문자가 포함된 경우 직접 따옴표로 감싸 주세요.
사용 시점
Find every occurrence of a function, error code or config key across a directory tree.
상세 분석
- -r
- recurse into subdirectories
- -n
- prefix matches with 1-based line numbers
- <pattern>
- search regex or text
- <dir>
- directory to search (use `.` for current)
예제
printf "200 GET /api\n500 POST /pay\n200 GET /api\n404 GET /old\n" | grep -n "GET"결과
1:200 GET /api 3:200 GET /api 4:404 GET /old대체 방법
- rg "<pattern>" (ripgrep, skips .gitignore automatically)
참고
Transform text streams (sed)
sed edits lines on the fly. Test without `-i` first so you preview changes on stdout before modifying files.
공식 문서: sed commandsProcess columns & aggregate (awk)
awk splits every line into fields ($1, $2, ...) automatically, running `{ action }` whenever `condition` is true.
공식 문서: GNU Awk User GuideSort, count, cut & translate
Fast single-purpose coreutils for frequency counts, column slicing and character cleanup.
공식 문서: Operating on sorted filesFind files & batch commands (find, xargs)
Locate files by name, size or age and pass them safely to downstream commands using NUL delimiters.
공식 문서: GNU Findutils ManualKitLocal 도구에서 사용해 보기
용어집
- stdin / stdout
- Standard input and output streams wired together by pipes (`|`).
- ERE (-E)
- Extended Regular Expressions: `+`, `?`, `|`, `()` work without extra backslashes.
- Field ($1, $NF)
- In awk, columns split by whitespace (or `-F`): `$0` is the whole line, `$NF` is the last column.
- NUL delimiter (-0)
- Separating paths with `\0` instead of newlines so spaces and quotes never break pipelines.