sed/awk/grep Cheatsheet - Linux Text Processing Reference
All essential sed/awk/grep commands organized by use case, with 52+ entries you can copy and run directly. Find the right command fast when you need it.
Back to SysOpsgrep 10
grep "error" app.logFindpackage含 error 的行
grep -i "error" app.log忽略size写Match
grep -rn "TODO" src/recursivelySearchDirectory,DisplayFile名和行号
grep -v "debug" app.log反向Match,排除含 debug 的行
grep -c "error" app.log统计Match行数
grep -E "error|warning" app.log扩展正则,equivalent to egrep
grep -oE "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" access.log只OutputMatch的 IP 地址部分
grep -A 3 "error" app.logMatch行后再Display 3 行上下文
grep -B 2 "error" app.logMatch行前Display 2 行上下文
grep -f pattern.txt app.log从FileRead多个模式
sed 9
sed 's/old/new/g' file.txt全局Replace old 为 new
sed -i 's/old/new/g' file.txt直接修改File(原地Edit)
sed -i.bak 's/old/new/g' file.txt修改File并backup原File为 .bak
sed '5d' file.txtDeleteline 5
sed '/^#/d' file.confDelete所有以 # beginning的注释行
sed '/^$/d' file.txtDelete空行
sed -n '10,20p' file.txt只打印第 10-20 行
sed 's/[[:space:]]*$//' file.txtDeleteline end空格
sed -E 's/([0-9]+)/\1/' file.txt扩展正则catch组,-E equivalent to -r
awk 9
awk '{print $1}' access.log打印第一列(默认空格分隔)
awk -F: '{print $1, $3}' /etc/passwd指定冒号分隔,打印User名和 UID
awk 'NR==10' file.txt打印line 10(NR=行号)
awk 'NR>=10 && NR<=20' file.txt打印第 10-20 行
awk '{sum += $1} END {print sum}' nums.txt对第一列sum
awk '{count[$1]++} END {for(k in count) print k, count[k]}' access.log按第一列group统计出现次数
awk '$3 > 100' data.txt打印第三列大于 100 的行
awk 'length > 80' file.txt打印length超过 80 的行
awk -F: '$3 >= 1000 {print $1}' /etc/passwdList UID >= 1000 的普通User
Pipeline 4
grep "error" app.log | awk "{print \$1}" | sort | uniq -c | sort -rn | head统计 error 日志中每个时间戳出现次数 TOP
cat access.log | awk "{print \$1}" | sort | uniq -c | sort -rn | head -20统计访问量 TOP 20 的 IP
grep -oE "[0-9]+ms" app.log | sort -rn | head -10提取response耗时 TOP 10
sed '/^#/d; /^$/d' nginx.conf一次Delete注释行和空行(多条命令用分号)
sort / uniq / comm 10
sort file.txt对File内容sort(默认按dictionary序)
sort -n file.txt按数值sizesort
sort -r file.txtreversesort
sort -t: -k3 -n /etc/passwd按冒号分隔的第 3 列数值sort
sort -u file.txtsort并dedupe
sort file.txt | uniq -c | sort -rn | head经典组合:统计各行频率 TOP
uniq -c file.txt统计每行重复次数(需先 sort)
uniq -d file.txt只Display重复的行
comm file1 file2compare两个已sortFile,三列:仅 file1/仅 file2/共有
diff -u file1 file2统一格式compareFile差异
cut / tr / paste 10
cut -d: -f1 /etc/passwd按冒号分隔提取第 1 列
cut -c1-10 file.txt按字符位置slice第 1-10 characters
cut -f1,3 file.txt提取第 1 和第 3 列(默认 Tab 分隔)
tr 'a-z' 'A-Z' < file.txt小写转大写
tr -d ' \t' < file.txtDelete所有空格和 Tab
tr -s '\n' < file.txtCompress连续空行为单个换行
paste file1.txt file2.txt按列merge两个File(默认 Tab 分隔)
column -t file.txt将内容对齐为表格格式Output
expand file.txt将 Tab 转换is empty格
command | tee output.txt同时Output到屏幕和File(常用于pipe)
💡 Tips
- sed -i 直接修改File前先backup(-i.bak),或先不加 -i 预览结果再Write。
- grep 处理大File加 --line-buffered 让Output实时刷新,否则pipe下游会等buffered满。
- awk 单引号内用 \$1 取列,双引号内要转义为 \\$1,否则 shell 会解释。
Official References
Commands are compiled from the official docs below. Click to verify the latest usage.
Maintained by LaoHand
Publicly updated on Jul 21, 2026, continuously proofread against official docs.
Found an error? Report it
Wrong command or description? Open an issue to help us fix it.
Found an error? Report it