跳转到主要内容
kitlocal.dev

Text CLI (grep / sed / awk) 速查表

本地运行

100% 在你的浏览器中处理。输入和输出绝不会离开你的设备。

结合真实管道的 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 条命令

键盘快捷键/聚焦搜索框JK移动选择C复制选中命令F收藏选中项Esc清空搜索

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 commands

Process columns & aggregate (awk)

awk splits every line into fields ($1, $2, ...) automatically, running `{ action }` whenever `condition` is true.

官方文档:GNU Awk User Guide

Sort, count, cut & translate

Fast single-purpose coreutils for frequency counts, column slicing and character cleanup.

官方文档:Operating on sorted files

Find files & batch commands (find, xargs)

Locate files by name, size or age and pass them safely to downstream commands using NUL delimiters.

官方文档:GNU Findutils Manual

在 KitLocal 工具中试用

术语表
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.