当前位置:网站首页>shell之sed
shell之sed
2022-08-11 08:23:00 【1701y】
sed
sed(Stream EDitor)是一个强大而简单的文本解析转换工具,可以读取文本,并根据指定的条件对文本内容进行编辑(删除、替换、添加、移动等),最后输出所有行或者仅输出处理的某些行。sed 也可以在无交互的情况下实现相当复杂的文本处理操作,被广泛应用于 Shell 脚本中,用以完成各种自动化处理任务。
工作流程
读取:sed 从输入流(文件、管道、标准输入)中读取一行内容并存储到临时的缓冲区中(又称模式空间,patternspace)
执行:默认情况下,所有的 sed 命令都在模式空间中顺序地执行,除非指定了行的地址,否则 sed 命令将会在所有的行上依次执行。
显示:发送修改后的内容到输出流。再发送数据后,模式空间将会被清空。
在所有的文件内容都被处理完成之前,上述过程将重复执行,直至所有内容被处理完。
基本语法
基本格式:
sed [选项] ‘操作’ 参数
sed [选项] -f scriptfile 参数
选项
选项 | 作用 |
---|---|
-e | 表示用指定命令或者脚本来处理输入的文本文件,只有一个编辑命令的时候可以省略 |
-f | 表示用指定的脚本文件来处理输入的文本文件。 |
-n | 表示仅显示处理后的结果 |
-i.brk | 直接编辑文本文件,不输出结果 |
-r,-E | 使用扩展正则表达式 |
-s | 将多个文件视为独立文件,而不是单个连续的长文件流 |
常用命令格式
操作 | 作用 |
---|---|
a | 在当前行下面添加一行 |
c | 选定行替换为指定内容(整行内容) |
d | 删除选定的行 |
i | 选定行上面插入一行指定内容 |
p | 打印,输出指定行 |
s | 替换,替换指定字符,格式:“行范围 s/旧字符串/新字符串” |
y | 字符转换 |
r | 指定读取文件 |
w | 保存为文件 |
用法示例
[[email protected] sed]#sed -n 'p' passwd #输出所有内容,等同于 cat
[[email protected] sed]#sed -n '1p' passwd #输出第 1 行
root:x:0:0:root:/root:/bin/bash
[[email protected] sed]#sed -n '1,6p' passwd #输出第1到6行
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
[[email protected] sed]#sed -n 'p;n' passwd #输出所有奇数行
[[email protected] sed]#sed -n '1~2p' passwd
[[email protected] sed]#sed -n 'n;p' passwd #输出所有偶数行
[[email protected] sed]#sed -n '2~2p' passwd
[[email protected] sed]#sed -n '1,9{p;n}' passwd #输出1到9行之间的奇数行
[[email protected] sed]#sed -n '2,+3p' passwd #输出从第二行开始下面3行内容
[[email protected] sed]#sed -n '/root/p' passwd #输出包含root的行
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] sed]#sed -n '2,/bin/p' passwd #输出从第二行开始到包含bin的行的内容
[[email protected] sed]#sed -n '/root/=' passwd #输出包含root的行号
[[email protected] sed]#sed -n '/^root/p' passwd #输出以root开头的行
root:x:0:0:root:/root:/bin/bash
[[email protected] sed]#sed -n '/bash$/p' passwd #输出以bash结尾的行
root:x:0:0:root:/root:/bin/bash
tangkun:x:1000:1000:tangkun:/home/tangkun:/bin/bash
[[email protected] sed]#sed -n '/\<root\>/p' passwd #输出包含root单词的行
[[email protected] sed]#cat 2.txt
a
b
c
d
e
f
g
e
e
e
h
i
j
k
[[email protected] sed]#sed '/a/i 123' 2.txt #在含有a的行上面添加一行123
123
a
b
c
d
e
f
g
e
e
e
h
i
j
k
[[email protected] sed]#sed '/a/a 123' 2.txt #在含有a的行下面添加一行123
a
123
b
c
d
e
f
g
e
e
e
h
i
j
k
[[email protected] sed]#cat 2.txt |sed '3d' #删除第3行内容
[[email protected] sed]#cat 2.txt |sed '2,8d' #删除2到8行内容
[[email protected] sed]#sed '/^e/d' 2.txt #删除以e开头的行的内容
[[email protected] sed]#sed 's/a/A/' 3.txt #将每行的第一个a替换成A
[[email protected] sed]#sed 's/a/A/3' 3.txt #将每行的第三个a替换成A
[[email protected] sed]#sed 's/a/A/g' 3.txt #将所有的a替换成A
[[email protected] sed]#sed 's/^/#/g' 3.txt #在每一行的开头加上#
[[email protected] sed]#sed '/a/s/^/#/g' 3.txt #在含a的行的开头加上#
[[email protected] sed]#echo '123456789'|sed -r 's/(123)(456)(789)/\1/' #分组取出第一组
123
[[email protected] sed]#echo '123456789'|sed -r 's/(123)(456)(789)/\2/' #分组取出第二组
456
[[email protected] sed]#echo '123456789'|sed -r 's/(123)(456)(789)/\3/' #分组取出第三组
789
边栏推荐
- leetcode:69. x 的平方根
- 记录一些遇见的bug——Lombok和Mapstruct的冲突导致,A component required a bean of type ‘com.XXX.controller.converter.
- Break pad source code compilation--refer to the summary of the big blogger
- 机器学习(一)数据的预处理
- Keep track of your monthly income and expenses through bookkeeping
- Essential C# scripting skills for Unity developers
- Use tf.argmax in Tensorflow to return the index of the maximum value of the tensor along the specified dimension
- 查找最新人员工资和上上次人员工资的变动情况
- Initial use of IDEA
- 装饰器模式:Swift 实现
猜你喜欢
FPGA 20个例程篇:11.USB2.0接收并回复CRC16位校验
1.1-Regression
golang 字符串操作
JUC并发编程
Do you know the basic process and use case design method of interface testing?
Project 1 - PM2.5 Forecast
分布式锁-Redission - 缓存一致性解决
Redis 只会用缓存?20种妙用让同事直呼牛X(荣耀典藏版)
Nuget can't find the package problem
One network cable to transfer files between two computers
随机推荐
Filesystem Hierarchy Standard
TF generates (feature, label) set through feature and label, tf.data.Dataset.from_tensor_slices
用 Antlr 重构脚本解释器
go 操作MySQL之mysql包
Hibernate 的 Session 缓存相关操作
Unity3D - modification of the Inspector panel of the custom class
经典论文-MobileNet V1论文及实践
golang 字符串操作
迷你图书馆系统(对象+数组)
Kotlin算法入门计算水仙花数
基于微信小程序的租房小程序
机器学习(一)数据的预处理
C语言-结构体
matplotlib
go-grpc TSL authentication solution transport: authentication handshake failed: x509 certificate relies on ... ...
机器学习(二)线性回归
leetcode: 69. Square root of x
研发了 5 年的时序数据库,到底要解决什么问题?
JUC Concurrent Programming
【LeetCode】链表题解汇总