跳至主要內容
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.