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

边栏推荐
- opengauss创建用户权限问题
- Kotlin算法入门计算水仙花数
- 【43. 字符串相乘】
- Conditional statements in TF; where()
- TF generates (feature, label) set through feature and label, tf.data.Dataset.from_tensor_slices
- 记录一些遇见的bug——Lombok和Mapstruct的冲突导致,A component required a bean of type ‘com.XXX.controller.converter.
- 笔试题大疆08.07
- 如何通过开源数据库管理工具 DBeaver 连接 TDengine
- 你有对象类,我有结构体,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang结构体(struct)的使用EP06
- [C语言] sscanf如何实现sscanf_s?
猜你喜欢

3.2 - classification - Logistic regression

IQUNIX A80 exploring TTC金粉 初体验

Use tf.argmax in Tensorflow to return the index of the maximum value of the tensor along the specified dimension

Square, multi-power, square root calculation in Tf

Unity3D - modification of the Inspector panel of the custom class

Analysys and the Alliance of Small and Medium Banks jointly released the Hainan Digital Economy Index, so stay tuned!

【实战系列】OpenApi设计规范

leetcode: 69. Square root of x

leetcode:69. x 的平方根

笔试题大疆08.07
随机推荐
Active users of mobile banking grew rapidly in June, hitting a half-year high
Decrement operation in tf; tf.assign_sub()
LoRa芯片的特征
分布式锁-Redission - 缓存一致性解决
golang 字符串操作
小目标检测3_注意力机制_Self-Attention
Getting Started with Kotlin Algorithm to Calculate the Number of Daffodils
进阶-指针
如何通过开源数据库管理工具 DBeaver 连接 TDengine
链式编程注解
2022-08-10 mysql/stonedb-slow SQL-Q16-time-consuming tracking
2022-08-10:为了给刷题的同学一些奖励,力扣团队引入了一个弹簧游戏机, 游戏机由 N 个特殊弹簧排成一排,编号为 0 到 N-1, 初始有一个小球在编号 0 的弹簧处。若小球在编号为 i 的弹
9、Neural Sparse Voxel Fields
Pico neo3 Unity Packaging Settings
场地预订系统,帮助场馆提高坪效
Features of LoRa Chips
The softmax function is used in TF;
Linux,Redis中IOException: 远程主机强迫关闭了一个现有的连接。解决方法
研发了 5 年的时序数据库,到底要解决什么问题?
kali渗透测试环境搭建