当前位置: 首页 > 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.lryc.cn/news/627031.html

相关文章:

  • MYSQL-增删查改CRUD
  • uni-app跨端开发最后一公里:详解应用上架各大应用商店全流程
  • 生产级的雪花算法
  • 自动驾驶导航信号使用方式调研
  • C语言实现全排列(非递归法)(以猪八戒买包子的故事为例解释)
  • SpringBoot 整合 Langchain4j RAG 技术深度使用解析
  • imx6ull-驱动开发篇30——Linux 非阻塞IO实验
  • redis---常用数据类型及内部编码
  • 设计具有功能安全和网络安全能力的新型半导体芯片
  • 攻克PostgreSQL专家认证
  • Unicode 字符串转 UTF-8 编码算法剖析
  • JVM面试精选 20 题(终)
  • SQL count(*)与 sum 区别
  • 第三阶段数据-4:SqlHelper类,数据库删除,DataTable创建
  • STM32F4 内存管理介绍及应用
  • 建模工具Sparx EA的多视图协作教程
  • PyTorch - Developer Notes
  • 吴恩达 Machine Learning(Class 3)
  • 国产化PDF处理控件Spire.PDF教程:如何使用 Python 添加水印到 PDF
  • Linux命令大全-ps命令
  • Linux系统之部署nullboard任务管理工具
  • 基于springboot中学信息技术课程教学网站
  • 栈上创建和堆上创建区别
  • Nginx 的完整配置文件结构、配置语法以及模块详解
  • 设计模式1-单例模式
  • 继续记事本项目
  • 盲盒商城h5源码搭建可二开幸运盲盒回收转增定制开发教程
  • Hyperledger Fabric官方中文教程-改进笔记(十三)-使用测试网络创建通道
  • Google Chrome 扩展不受信任 - 不受支持的清单版本 解决方案
  • 整体设计 之定稿 “凝聚式中心点”原型 --整除:智能合约和DBMS的在表层挂接 能/所 依据的深层套接