0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

??sed命令从入门到实战

马哥Linux运维 ? 来源:博客园 ? 2025-06-18 15:59 ? 次阅读
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

sed 命令详解及示例

sed是一种流编辑器,能高效地完成各种替换、删除、插入等操作,按照文件数据行顺序,重复处理满足条件的每一行数据,然后把结果展示打印,且不会改变原文件内容。

sed会逐行扫描输入的数据,并将读取的数据内容复制到临时缓冲区中,称为“模式空间”(pattern space),然后拿模式空间中的数据与给定的条件进行匹配,如果匹配成功,则执行特定的sed指令,否则跳过输入的数据行,继续读取后面的数据。

一、命令介绍

1.1 命令语法

|   |   |
| --- | --- |
|   | sed [OPTION]... {script-only-if-no-other-script} [input-file]... |
|   |   |
|   |#如: |
|   | sed [选项]'匹配条件和操作指令'文件名 |
|   |cat文件名 | sed [选项]'匹配条件和操作指令'|

1.2 选项参数

|   |   |
| --- | --- |
|   | -n, --quiet, --silent |
|   |#suppress automatic printing of pattern space |
|   | --debug  #annotate program execution |
|   | -e script, --expression=script |
|   |#add the script to the commands to be executed |
|   | -f script-file, --file=script-file |
|   |#add the contents of script-file to the commands to be executed |
|   | --follow-symlinks |
|   |#follow symlinks when processing in place |
|   | -i[SUFFIX], --in-place[=SUFFIX] |
|   |#edit files in place (makes backup if SUFFIX supplied) |
|   | -l N, --line-length=N |
|   |#specify the desired line-wrap length for the 'l' command |
|   | --posix    #disable all GNU extensions. |
|   | -E, -r, --regexp-extended |
|   |#use extended regular expressions in the script (for portability use POSIX -E). |
|   | -s, --separate |
|   |#consider files as separate rather than as a single, continuous long stream. |
|   | --sandbox |
|   |#operate in sandbox mode (disable e/r/w commands). |
|   | -u, --unbuffered |
|   |#load minimal amounts of data from the input files and flush the output buffers more often |
|   | -z, --null-data |
|   |#separate lines by NUL characters |
|   | --help  #display this help and exit |
|   | --version #output version information and exit |

选项 例子
-n, --quiet, --silent
禁止自动打印模式(常配合'p'使用,仅显示处理后的结果)
sed -n '/hello/p' filename
使用 /hello/ 匹配含有 "hello" 的行,p 打印匹配的行
--debug
以注解的方式显示 sed 的执行过程,帮助调试脚本
sed --debug 's/foo/bar/' filename
当你使用 sed 修改内容时,它会显示调试信息,以便你了解脚本是如何执行的
-e script, --expression=script
在命令行中直接指定 sed 脚本(允许多个 sed 表达式)
sed -e 's/foo/bar/' -e 's/hello/world/' filename
将文件中的 foo 替换为 bar,然后将 hello 替换为 world
-f script-file, --file=script-file
从指定的脚本文件中读取 sed 命令
sed -f script.sed filename
script.sed 是包含多个 sed 命令的脚本文件,sed 会按顺序执行这些命令
--follow-symlinks
当指定 -i 时,sed 会跟随符号链接(symlink)指向的实际文件进行编辑
sed -i --follow-symlinks 's/foo/bar/' symlink.txt
如果 symlink.txt 是一个符号链接文件,sed 会编辑它指向的实际文件
-i[SUFFIX], --in-place[=SUFFIX]
直接编辑文件(如果提供 SUFFIX,则进行备份)
sed -i.bak 's/foo/bar/' filename
直接在 filename 中将 foo 替换为 bar,并创建一个备份文件 filename.bak
-l N, --line-length=N
当使用 l 命令(列出行内容)时,指定输出的行宽(N 表示字符数)
echo 'hello world' | sed -l 5 'l'
使用 l 命令显示 "hello world",但每行最多显示 5 个字符
--posix
禁用 GNU 扩展,使 sed 遵循 POSIX 标准语法
sed --posix 's/foo/bar/' filename
这将禁用 sed 的一些非标准特性,确保脚本在 POSIX 环境下工作
-E, -r, --regexp-extended
使用扩展的正则表达式(ERE),这与基本正则表达式(BRE)相比,简化了一些语法(例如不用转义括号和 +)
echo "abc123" | sed -E 's/[a-z]+([0-9]+)/1/'
使用扩展正则表达式,匹配并提取字母后面的数字
-s, --separate
将多个输入文件视为独立的流,而不是作为一个连续的流处理
sed -s 's/foo/bar/' file1.txt file2.txt
sed 会分别处理 file1.txt 和 file2.txt,而不是将它们作为一个整体处理
--sandbox
以沙盒模式运行,禁止使用 e, r, w 命令,防止 sed 修改文件或执行外部命令
sed --sandbox 's/foo/bar/' filename
启用沙盒模式,防止 sed 脚本执行危险的操作
-u, --unbuffered
减少从输入文件读取数据时的缓冲区大小,并更频繁地刷新输出
sed -u 's/foo/bar/' filename
立即将处理结果输出到标准输出,而不是等到处理大量数据后再输出
-z, --null-data
将输入中的行分隔符从换行符 改为 NUL 字符 ?,这在处理二进制数据或以 NUL 作为分隔符的文本时很有用
sed -z 's/foo/bar/' filename
使用 NUL 字符作为行分隔符处理文本

1.3 匹配条件

格式 描述
/regexp/ 使用 "正则表达式",匹配数据行
n(数字) 使用 "行号" 匹配,范围是1-$($ 表示最后一行)
addr1,addr2 使用 "行号或正则" 定位,匹配从 addr1 到 addr2 的所有行
addr1,+n 使用 "行号或正则" 定位,匹配从 addr1 开始及后面的 n 行
n~step 使用 "行号",匹配从行号 n 开始,步长为 step 的所有数据行

1.3.1 定界符

/ 在sed中作为定界符使用,也可以使用任意的定界符。

|   |   |
| --- | --- |
|   | sed's|foo|bar|g'filename |
|   | sed'sbar:g'filename |
|   |   |
|   |#定界符出现在样式内部时,需要进行转义: |
|   | sed's//bin//usr/bin/g'filename |

1.3.2 变量引用

sed表达式使用单引号来引用,但是如果表达式内部包含"变量"字符串,则需要使用双引号。

|   |   |
| --- | --- |
|   | foo="world"|
|   |echo"hello world"| sed"s/$foo/librarookie"|
|   | hello librarookie |

1.4 操作指令

指令 描述 例子
!