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.

SysOps·52 commands·Last updated 2026-07-21
Back to SysOps

grep 10

grep "error" app.log
Findpackage含 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.log
Match行后再Display 3 行上下文
grep -B 2 "error" app.log
Match行前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.txt
Deleteline 5
sed '/^#/d' file.conf
Delete所有以 # beginning的注释行
sed '/^$/d' file.txt
Delete空行
sed -n '10,20p' file.txt
只打印第 10-20 行
sed 's/[[:space:]]*$//' file.txt
Deleteline 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/passwd
List 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.txt
reversesort
sort -t: -k3 -n /etc/passwd
按冒号分隔的第 3 列数值sort
sort -u file.txt
sort并dedupe
sort file.txt | uniq -c | sort -rn | head
经典组合:统计各行频率 TOP
uniq -c file.txt
统计每行重复次数(需先 sort)
uniq -d file.txt
只Display重复的行
comm file1 file2
compare两个已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.txt
Delete所有空格和 Tab
tr -s '\n' < file.txt
Compress连续空行为单个换行
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