Sed 流编辑器

精选 Sed 流编辑器 常用指令与核心速查备忘单,涵盖高频用法、配置参数与实用技巧。

#入门指南

#Sed 用法 (Sed Usage)

语法

$ sed [options] command [input-file]

With pipeline

$ cat report.txt | sed 's/Nick/John/g'
$ echo '123abc' | sed 's/[0-9]+//g'

#Option Examples

Option Example Description
-i sed -ibak 's/On/Off/' php.ini Backup and modify input file directly
-E sed -E 's/[0-9]+//g' input-file Use extended regular expressions
-n sed -n '3 p' config.conf Suppress default pattern space printing
-f sed -f script.sed config.conf Execute sed script file
-e sed -e 'command1' -e 'command2' input-file Execute multiple sed commands

#Multiple commands

$ echo "hello world" | sed -e 's/h/H/g' -e 's/w/W/g'
Hello World

Use -e to execute multiple sed commands

#Sed script

$ echo 's/h/H/g' >> hello.sed
$ echo 's/w/W/g' >> hello.sed
$ echo "hello world" | sed -f hello.sed
Hello World

Use -f to execute sed script file

#💡 实用示例

$ sed 's/old/new/g' file.txt
$ sed 's/old/new/g' file.txt > new.txt

$ sed 's/old/new/g' -i file.txt
$ sed 's/old/new/g' -i.backup file.txt

参阅: Sed examples

#Sed commands

标志位 描述与效果
g 全局替换整行所有匹配项 (Global)
1,2... 仅替换第 n 次出现的匹配项
p 仅打印成功发生替换的行
w 将替换后的行独立写入保存至指定文件
I 检索时忽略大小写 (Ignore case)
e 将替换后的结果当作命令行指令直接执行

#循环与分支指令 (Loops commands)

命令指令 含义与功能说明
b label 无条件跳转到指定标签 (用于构建循环结构)
t label 仅当上一次替换 s 成功时,跳转到指定标签
:label bt 跳转指令定义目标标签名
N 读取下一行追加到当前模式空间中 (生成多行文本)
P 仅打印多行模式空间中的第一行
D 仅删除多行模式空间中的第一行

#杂项与匹配替换控制 (Misc Flags)

标记符号 描述与效果说明
/ | ^ @ ! # 替换命令 s 的分隔符不仅限 /,可以为任意字符 (如 s@old@new@g)
& 匹配到的原始模式文本占位符
( ) \1 \2 \3 使用 () 定义捕获组,在替换文本中使用 \1, \2 引用捕获内容

#Sed 实战案例 (Sed examples)

#文本替换实战 (Replacing text)

替换整行中所有出现的字符串

$ sed 's/old/new/g' file.txt

仅替换整行中第 2 次出现的字符串

$ sed 's/old/new/2' file.txt

仅对第 5 行执行文本替换

$ sed '5 s/old/new/' file.txt

仅当行首包含 "hello" 时,才将 "world" 替换为 "universe"

$ sed '/hello/s/world/universe/' file.txt

删除每行末尾的转义反斜杠 ""

$ sed 's/\\$//' file.txt

去除每行开头的全部空白字符 (前导空格/Tab)

$ sed 's/^\s*//' file.txt

移除代码/配置文件注释(包括出现在行尾的注释)

$ sed 's/#.*$//' file.txt

#🔍 文本检索与筛选 (Search for text)

搜索字符串并仅打印成功匹配到的行

$ sed -n '/hello/p' file.txt

忽略大小写进行搜索与打印

$ sed -n '/hello/Ip' file.txt

搜索匹配项,但仅输出未匹配的行记录

$ sed -n '/hello/!p' file.txt

#行后追加内容 (Appending lines)

在第 2 行下方追加新行文本

$ sed '2a Text after line 2' file.txt

在文件末尾追加新行文本

$ sed '$a THE END!' file.txt

从第 3 行开始,每隔 3 行追加一行文本

$ sed '3~3a Some text' file.txt

#为文本生成行号 (Numbering)

为文件打印行号 (左对齐基础格式)

$ sed = file.txt | sed 'N;s/\n/\t/'

为文件打印行号 (数字靠左,右对齐美化)

$ sed = file.txt | sed 'N; s/^/   /; s/ *\(.\{6,\}\)\n/\1  /'

为非空行打印行号 (遇到空行跳过行号)

$ sed '/./=' file.txt | sed '/./N; s/\n/ /'

统计计算总行数 (模拟 wc -l 功能)

$ sed -n '$='

#行前插入内容 (Prepending lines)

在第 5 行上方插入文本

$ sed '5i line number five' file.txt

在所有包含 "hello" 的行上方插入 "Example: "

$ sed '/hello/i Example: ' file.txt

#文本行删除 (Deleting lines)

删除文件中第 5 行至第 7 行

$ sed '5,7d' file.txt

从第 3 行开始,每隔 2 行删除一行

$ sed '3~2d' file.txt

删除文件中的最后一行

$ sed '$d' file.txt

删除所有以 "Hello" 开头的行

$ sed '/^Hello/d' file.txt

删除全文所有空行

$ sed '/^$/d' file.txt

删除所有以 "#" 开头的注释行

$ sed '/^#/d' file.txt

#空行与间距控制 (File spacing)

每一行后插入一个空行 (双倍行距)

$ sed G

删除现有所有空行,并统一设置为双倍行距

$ sed '/^$/d;G'

设置三倍行距

$ sed 'G;G'

取消双倍行距 (还原为单倍)

$ sed 'n;d'

在匹配正则 "regex" 的行上方插入一个空行

$ sed '/regex/{x;p;x;}'

在匹配正则 "regex" 的行下方插入一个空行

$ sed '/regex/G'

在匹配正则 "regex" 的行上下两侧各插入一个空行

$ sed '/regex/{x;p;x;G;}'

#🔗 参考资源