当前位置:网站首页>shell脚本详解(十)——sed编辑器的使用方法
shell脚本详解(十)——sed编辑器的使用方法
2022-06-22 17:55:00 【成为最好的梅西】
目录
一、sed编辑器
- sed是一种流编辑器,流编辑器会在编辑器处理数据之前基于预先提供的一组规则来编辑数据流。
- sed编辑器可以根据命令来处理数据流中的数据,这些命令要么从命令行中输入,要么存储在一个命令文本文件中。
二、sed编辑器工作流程
1、读取:
- sed从输入流 (文件、管道、标准输入) 中读取一行内容并存储到临时的缓冲区中(又称模式空间,pattern space)
2、执行:
- 默认情况下,所有的sed命令都在模式空间中顺序地执行,除非指定了行的地址,否则sed命令将会在所有的行上依次执行。
3、显示:
- 发送修改后的内容到输出流。在发送数据后,模式空间将会被清空。在所有的文件内容都被处理完成之前,上述过程将重复执行,直至所有内容被处理完。
4、注:
- 在所有的文件内容都被处理完成之前,上述过程将重复执行,直至所有内容被处理完。
- 默认情况下所有的sed命令都是在模式空间内执行的,因此输入的文件并不会发生任何变化,除非是用重定向存储输出。
三、命令格式

四、常用选项
| 选项 | 说明 |
|---|---|
| -e或- -expression= | 表示用指定命令来处理输入的文本文件,只有一个操作命令时可省略,一 般在执行多个操作命令使用 |
| f或- -file= | 表示用指定的脚本文件来处理输入的文本文件 |
| h或- -help | 显示帮助 |
| -n、- -quiet或silent | 禁止sed编辑器输出,但可以与p命令一起使用完成输出 |
| -i | 直接修改目标文本文件 |


五、常用操作
| 操作 | 说明 |
|---|---|
| s | 替换,替换指定字符 |
| d | 删除,删除选定的行 |
| a | 增加,在当前行下面增加一行指定内容 |
| i | 插入,在选定行,上面插入一行指定内容 |
| c | 替换,将选定行替换为指定内容 |
| y | 字符转换,转换前后的字符长度必须相同 |
| p | 打印,如果同时指定行,表示打印指定行;如果不指定行,则表示打印所有内容;如果有非打印字符,则以ASCII码输出。其通常与“-n”选项一起使用 |
| = | 打印行号 |
| l (小写L) | 打印数据流中的文本和不可打印的ASCII字符(比如结束符$、制表符\t) |
六、使用地址
sed编辑器有2种寻址方式:
- 以数字形式表示行区间
- 用文本模式来过滤出行
七、删除行

八、替换
1、格式

2、4种替换标记
- 数字:表明新字符串将替换第几处匹配的地方
- g:表明新字符将会替换所有匹配的地方
- p:打印与替换命令匹配的行,与-n一起使用
- w 文件:将替换的结果写到文件中


九、插入

边栏推荐
- std::enable_shared_from_this 错误:error: expected template-name before ‘<’ token
- 运维、监控、AIOps的几个重要观点
- Niuke.com: judge whether it is palindrome string
- Game NFT Market: opensea's most easily cut cake
- 什么?HomeKit、米家、Aqara等生态也能通过智汀与天猫精灵生态联动?
- Golang实现基于Redis的可靠延迟队列
- Method of activity jump to fragment (intent)
- Linked list 4- 21 merge two ordered linked lists
- Pre training language model, Bert, roformer SIM, also known as simbertv2
- 如何在 FlowUs和Notion 等笔记软件中进行任务管理?
猜你喜欢
随机推荐
贪心之区间问题(3)
Aiops intelligent operation and maintenance experience sharing
STM32 control matrix key, Hal library, cubemx configuration
2022 t elevator repair recurrent training question bank and answers
How to manage tasks in note taking software such as flowus and notation?
一些技术想法:
China's two meteorological "new stars" data products are shared with global users
Iplook becomes RedHat (red hat) business partner
ssh免密码登录
vs code突然无法进行代码跳转
Preliminary controller input of oculus learning notes (1)
Several important viewpoints on operation and maintenance, monitoring and aiops
缓存3种方式及原理
5G 短消息解决方案
数商云:数字化供应链系统搭建,赋能企业实现物流供应链的优化升级
Notes on new reports
Custom database connection pool class: requirement: enclose the collection class of a collection object
预约打新债哪个券商公司开户好,开账户是更安全呢
Iplook, as a member of o-ran alliance, will jointly promote the development of 5g industry
Some preliminary explorations of avoiding obstacles and finding paths by rays in unity







![jniLibs.srcDirs = [‘libs‘]有什么用?](/img/d5/3070f8e793507efc601bb22d5024fa.png)


