当前位置: 首页 > news >正文

【网络运维】Linux 文本处理利器:sed 命令

Linux 文本处理利器:sed 命令

sed 简介

sed(Stream Editor)是一款非交互式的流编辑器,诞生于 1973–1974 年间的贝尔实验室,由 McMahon 开发。它专为文本处理而生,功能强大,是 Linux 文本处理常客之一。

(另外两名常客通常视为 grep命令和 awk命令)

其中,sed 与 awk 并称 Linux/Unix 两大文字处理王牌工具,sed 侧重于替换,awk 侧重于分割与重组

本文将深入探讨 sed 命令的使用方法与实用技巧。


sed 工作流程

sed 的工作流程可简单概括为:

读取行 → 执行 → 显示 → 读取行 → 执行 → 显示 → ……(重复流程)

在这里插入图片描述

详细步骤:

  1. 读取行:从输入流(文件、管道、标准输入)读取一行存入 pattern space
  2. 执行:按顺序对当前行应用 sed 命令。
  3. 显示:将处理后的数据发送到输出流,并清空 pattern space
  4. 循环:重复上述过程直到所有输入处理完毕。

注意事项:

  • pattern space 是内存中的临时区域,关机或关闭终端后数据丢失。
  • 默认不修改源文件,除非使用 -i 选项。
  • hold space 是另一个内存区域,用于持久化存储数据,供后续检索使用。
  • 未指定输入文件时,sed 从标准输入读取数据。

环境准备

[furongwang@shell ~]$ vim test
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/false
daemon:x:2:2:daemon:/sbin:/bin/false
mail:x:8:12:mail:/var/spool/mail:/bin/false
ftp:x:14:11:ftp:/home/ftp:/bin/false
&nobody:$:99:99:nobody:/:/bin/false
zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
http:x:33:33::/srv/http:/bin/false
dbus:x:81:81:System message bus:/:/bin/false
hal:x:82:82:HAL daemon:/:/bin/false
mysql:x:89:89::/var/lib/mysql:/bin/false
aaa:x:1001:1001::/home/aaa:/bin/bash
ba:x:1002:1002::/home/zhangy:/bin/bash
test:x:1003:1003::/home/test:/bin/bash
@zhangying:*:1004:1004::/home/test:/bin/bash
policykit:x:102:1005:Po

sed 命令语法

基本格式:

sed [option] [sed-command] [input-file]

常用选项:

选项说明
-n禁止自动打印模式空间
-e添加要执行的脚本
-f从文件中读取脚本,脚本中的每一个 sed 命令独自成一行
-i直接修改源文件(可指定备份后缀)
-r使用扩展正则表达式

示例:

打印文件内容(模拟 cat):
sed '' data.txt(任意文件名)
[furongwang@shell ~]$ cat data.txt 
I am studing sed
I am www.twle.cn
I am a no-work-men
I am so handsome[furongwang@shell ~]$ sed '' data.txt 
I am studing sed
I am www.twle.cn
I am a no-work-men
I am so handsome
从标准输入读取:
sed ''# 输入内容后按 Ctrl+D 结束
[furongwang@shell ~]$ sed ''
# 输入hello world,并回车
hello world
# 输出hello world
hello world
# 按ctrl+d推出

行寻址与模式寻址

行寻址语法:

[address1[,address2]]命令

示例:

# 打印第1行
[furongwang@shell ~]$ sed -n '1p' test# 打印第1到3行
[furongwang@shell ~]$ sed -n '1,3p' test# 打印第3行到最后
[furongwang@shell ~]$ sed -n '3,$p' test# 打印第2行及后面2行
[furongwang@shell ~]$ sed -n '2,+2p' test# 每隔2行打印一次
[furongwang@shell ~]$ sed -n '1~2p' test

模式寻址:

# 打印含 "zhang" 的行
[furongwang@shell ~]$ sed -n '/zhang/p' test# 打印从 root 开头到 mail 开头的行
[furongwang@shell ~]$ sed -n '/^root/,/^mail/p' test

常用子命令详解

1. 打印(p / P)

  • p:打印整个模式空间
  • P:打印模式空间的第一行
[furongwang@shell ~]$ sed -n '1p' test          # 打印第1行
[furongwang@shell ~]$ sed -n '1,3p' test        # 打印1~3行

2. 读取下一行(n / N)

  • n:读取下一行覆盖当前行
  • N:追加下一行到当前行(用 \n 连接)
# 打印偶数行
[furongwang@shell ~]$ sed -n 'n;p' test# 合并相邻两行
[furongwang@shell ~]$ sed 'N;s/\n/==/' test

3. 替换(s)

# 替换每行第一个 root
[furongwang@shell ~]$ sed 's/root/tankzhang/' test# 替换所有 root
[furongwang@shell ~]$ sed 's/root/tankzhang/g' test# 替换每行第3个 my
[furongwang@shell ~]$ sed 's/my/your/3' test1.txt# 自定义分隔符
[furongwang@shell ~]$ sed 's#root#hello#' test

4. 插入与追加(i / a)

# 在匹配行上方插入
[furongwang@shell ~]$ sed '/root/i====aaaa====' test# 在匹配行下方追加
[furongwang@shell ~]$ sed '/root/a====iiii====' test

5. 删除(d / D)

  • d:删除整个模式空间
  • D:删除模式空间的第一行
# 删除1~14行
[furongwang@shell ~]$ sed '1,14d' test# 删除含 false 或 bash 的行
[furongwang@shell ~]$ sed '/\(false\|bash\)/d' test

6. 写文件(w / W)

# 将 root 开头的行写入文件
[furongwang@shell ~]$ sed -n '/^root/w output.txt' test
[furongwang@shell ~]$ cat output.txt
root:x:0:0:root:/root:/bin/bash# 小写w写入
[furongwang@shell ~]$ vim scripts 
1{
N
w write.log
}
[furongwang@shell ~]$ sed -n -f scripts test# 小写w写入包含模式中所有行
[furongwang@shell ~]$ cat write.log
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/bin/false# 大写W写入只包含模式中第一行
[furongwang@shell ~]$ vim scripts 
1{
N
W write.log
}
[furongwang@shell ~]$ sed -n -f scripts test
[furongwang@shell ~]$ cat write.log
root:x:0:0:root:/root:/bin/bash

7. 退出(q)

# 处理到第3行退出
[furongwang@shell ~]$ sed '3q' test

8. 取反(!)

# 打印非 root 开头的行
[furongwang@shell ~]$ sed -n '/^root/!p' test

高级用法

多命令执行:

# 使用分号分隔
[furongwang@shell ~]$ sed '1d;2d;5d' test# 使用 -e 选项
[furongwang@shell ~]$ sed -e '1d' -e '2d' test# 使用脚本文件
[furongwang@shell ~]$ sed -f script.sed test

正则表达式与子串:

# 使用转义括号
[furongwang@shell ~]$ sed 's/\(root\)/\1user/g' test# 使用 -r 避免转义
[furongwang@shell ~]$ sed -r 's/(root)/\1user/g' test# 使用 & 代表匹配内容
[furongwang@shell ~]$ sed 's/root/&user/g' test

模式空间与保持空间(g/G/h/H):

命令说明
g用保持空间内容覆盖模式空间
G将保持空间内容追加到模式空间
h用模式空间内容覆盖保持空间
H将模式空间内容追加到保持空间
# 每行后加空行
[furongwang@shell ~]$ sed 'G' test# 复制匹配行到文件末尾
[furongwang@shell ~]$ sed -e '/root/h' -e '$G' test

附:sed 命令完整帮助信息

[furongwang@shell ~]$ sed --help
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...-n, --quiet, --silentsuppress automatic printing of pattern space-e script, --expression=scriptadd the script to the commands to be executed-f script-file, --file=script-fileadd the contents of script-file to the commands to be executed--follow-symlinksfollow symlinks when processing in place-i[SUFFIX], --in-place[=SUFFIX]edit files in place (makes backup if SUFFIX supplied)-c, --copyuse copy instead of rename when shuffling files in -i mode-b, --binarydoes nothing; for compatibility with WIN32/CYGWIN/MSDOS/EMX (open files in binary mode (CR+LFs are not treated specially))-l N, --line-length=Nspecify the desired line-wrap length for the `l' command--posixdisable all GNU extensions.-r, --regexp-extendeduse extended regular expressions in the script.-s, --separateconsider files as separate rather than as a single continuouslong stream.-u, --unspaceedload minimal amounts of data from the input files and flushthe output spaces more often-z, --null-dataseparate lines by NUL characters--helpdisplay this help and exit--versionoutput version information and exitIf no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
E-mail bug reports to: <bug-sed@gnu.org>.
Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.
 -n, --quiet, --silent    取消自动打印模式空间-e 脚本, --expression=脚本   添加“脚本”到程序的运行列表-f 脚本文件, --file=脚本文件  添加“脚本文件”到程序的运行列表--follow-symlinks    直接修改文件时跟随软链接-i[扩展名], --in-place[=扩展名]    直接修改文件(如果指定扩展名就备份文件)-l N, --line-length=N   指定“l”命令的换行期望长度--posix  关闭所有 GNU 扩展-r, --regexp-extended  在脚本中使用扩展正则表达式-s, --separate  将输入文件视为各个独立的文件而不是一个长的连续输入-u, --unspaceed  从输入文件读取最少的数据,更频繁的刷新输出--help     打印帮助并退出--version  输出版本信息并退出-a ∶新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)-c ∶取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!-d ∶删除,因为是删除啊,所以 d 后面通常不接任何咚咚;-i ∶插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行)-p ∶列印,亦即将某个选择的资料印出。通常 p 会与参数 sed -n 一起运作~-s ∶取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法
http://www.xdnf.cn/news/1334125.html

相关文章:

  • Obsidian 1.9.10升级
  • Lecture 6 Kernels, Triton 课程笔记
  • python-使用鼠标对图片进行涂抹自定义绘图
  • React框架超详细入门到实战项目演练【前端】【React】
  • 玳瑁的嵌入式日记D21-08020(数据结构)
  • 河南萌新联赛2025第六场 - 郑州大学
  • 一种数字相机中的自动曝光算法
  • Java 性能优化实战(二):JVM 调优的 5 个核心维度
  • ABAP OOP革命:ALV报表面向对象改造深度实战
  • 基于Python的反诈知识科普平台 Python+Django+Vue.js
  • 49 C++ STL模板库18-类模板-pair
  • 解决前端项目启动时找不到esm文件的问题
  • PostgreSQL 流程---更新
  • 力扣面试150(61/100)
  • 使用安卓平板,通过USB数据线(而不是Wi-Fi)来控制电脑(版本1)
  • 笔试——Day44
  • 使用RealSense相机和YOLO进行实时目标检测
  • 从零开发Java坦克大战Ⅱ(上) -- 从单机到联机(架构演进与设计模式剖析)
  • 01.初识mysql数据库,了解sql语句
  • React-native之组件
  • 《算法导论》第 34 章 - NP 完全性
  • J1939协议
  • C++围绕音视频相关的资料都有哪些?如何进行学习
  • 升级Android系统webview
  • 运维日常工作100条
  • linux内核源码下载
  • Redisson3.14.1及之后连接阿里云redis代理模式,使用分布式锁:ERR unknown command ‘WAIT‘
  • 双模式 RTMP H.265 播放器解析:从国内扩展到 Enhanced RTMP 标准的演进
  • 猫头虎开源AI分享|基于大模型和RAG的一款智能text2sql问答系统:SQLBot(SQL-RAG-QABot),可以帮你用自然语言查询数据库
  • PowerShell脚本检查业务健康状态