Cheatsheet de Text CLI (grep / sed / awk)
Comandos Unix de processamento de texto com pipelines reais: busca com GNU grep/ripgrep, edição em fluxo com sed, campos e agregações com awk, além de cut, sort, uniq e tr.
Como funciona
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 `|`.
Kit de sobrevivência
Os principais comandos para aprender primeiro.
41 comandos
Search lines (grep)
grep prints lines matching a pattern. Add -n for line numbers, -i for case-insensitive and -E for extended regex.
Documentação oficial: grep usage- Essencial
Search recursively and print file paths and line numbers.
grep -rn "<pattern>" <dir>Montar o comando
grep -rn "<pattern>" <dir>Os valores são inseridos exatamente como digitados. Coloque entre aspas você mesmo se contiverem espaços ou caracteres especiais.
Quando usar
Find every occurrence of a function, error code or config key across a directory tree.
Detalhamento
- -r
- recurse into subdirectories
- -n
- prefix matches with 1-based line numbers
- <pattern>
- search regex or text
- <dir>
- directory to search (use `.` for current)
Exemplo
printf "200 GET /api\n500 POST /pay\n200 GET /api\n404 GET /old\n" | grep -n "GET"Resultado
1:200 GET /api 3:200 GET /api 4:404 GET /oldTambém funciona
- rg "<pattern>" (ripgrep, skips .gitignore automatically)
Veja também
Transform text streams (sed)
sed edits lines on the fly. Test without `-i` first so you preview changes on stdout before modifying files.
Documentação oficial: sed commandsProcess columns & aggregate (awk)
awk splits every line into fields ($1, $2, ...) automatically, running `{ action }` whenever `condition` is true.
Documentação oficial: GNU Awk User GuideSort, count, cut & translate
Fast single-purpose coreutils for frequency counts, column slicing and character cleanup.
Documentação oficial: 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.
Documentação oficial: GNU Findutils ManualExperimente em uma ferramenta do KitLocal
Glossário
- 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.