当前位置:网站首页>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

边栏推荐
猜你喜欢
随机推荐
About # SQL problem: how to set the following data by commas into multiple lines, in the form of column display
4.1ROS运行管理/launch文件
兼容并蓄广纳百川,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang复合容器类型的声明和使用EP04
Swagger简单使用
机器学习(一)数据的预处理
Find the latest staff salary and the last staff salary changes
【LeetCode】链表题解汇总
Decrement operation in tf; tf.assign_sub()
2021-08-11 For loop combined with multi-threaded asynchronous query and collect results
剑指offer专项突击版第26天
tf.reduce_mean() and tf.reduce_sum()
Do you know the basic process and use case design method of interface testing?
Analysys and the Alliance of Small and Medium Banks jointly released the Hainan Digital Economy Index, so stay tuned!
Kotlin算法入门求自由落体
Kali penetration test environment set up
XXL-JOB 分布式任务调度中心搭建
【BM87 合并两个有序的数组】
3.1-Classification-probabilistic generative model
欢迎加入sumarua网络安全交流社区
装饰器模式:Swift 实现
![ASP.NET Core 6框架揭秘实例演示[32]:错误页面的集中呈现方式](/img/c9/93ab353c4908adaaae0da2cc3b6a3c.png)







