当前位置:网站首页>sed编辑器
sed编辑器
2022-06-26 12:40:00 【C和弦~】
目录
引言:
在工作中sed编辑器的使用很多,因此对于运维来说熟练掌握sed编辑器的使用自然也是很重要的。
一.sed编辑器
- sed是一种流编辑器,流编辑器会在编辑器处理数据之前基于预先提供的一组规则来编辑数据流。
- sed编辑器可以根据命令来处理数据流中的数据,这些命令要么从命令行中输入,要么存储在一个命令文本文件中。
二、sed编辑器工作流程
2.1 读取
- sed从输入流 (文件、管道、标准输入) 中读取一行内容并存储到临时的缓冲区中(又称模式空间,pattern space)
2.2 执行
- 默认情况下,所有的sed命令都在模式空间中顺序地执行,除非指定了行的地址,否则sed命令将会在所有的行上依次执行。
2.3 显示
- 发送修改后的内容到输出流。在发送数据后,模式空间将会被清空。在所有的文件内容都被处理完成之前,上述过程将重复执行,直至所有内容被处理完。
2.4 补充
- 在所有的文件内容都被处理完成之前,上述过程将重复执行,直至所有内容被处理完。
- 默认情况下所有的sed命令都是在模式空间内执行的,因此输入的文件并不会发生任何变化,除非是用重定向存储输出
三.命令格式
- sed [选项] '操作' 参数
- sed [选项] -f scriptfile 参数
常用选项:
- -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.用文本模式来过滤出行
五.删除、替换、插入
删除
- sed 'd' 1.txt #全部都删除
- sed '$d' 1.txt #删除最后一行
- sed '2,5d' 1.txt #删除2-5行
替换
格式:行范围 s/旧字符 /新字符 /替换标记
4种替换标记:1.数字:表明新字符串将替换第几处匹配的地方
2.g:表明新字符将会替换所有匹配的地方
3.p:打印与替换命令匹配的行,与-n一起使用
4.w 文件:将替换的结果写到文件中
插入
- sed '1a 520' 1.txt #在1.txt文件的第一行后插入520
- sed '1i 520' 1.txt #在1.txt文件的第一行前插入520
- sed '/r/etc/resolv.conf' 1.txt #在1.txt文件中第一行后插入文本
总结:
sed的使用很常见、也很重要,因此作为一名运维工程师必须熟练的掌握sed的用法和理论。
边栏推荐
- H - Sumsets POJ 2229
- HDU 3555 Bomb
- Bifu divides EtherCAT module into multiple synchronization units for operation -- use of sync units
- Solutions to insufficient display permissions of find and Du -sh
- Beifu realizes the control of time slice size and quantity through CTU and ton
- Processing function translate (mousex, mousey) learning
- Composite mode
- LeetCode_ Stack_ Medium_ 150. evaluation of inverse Polish expression
- J - Wooden Sticks poj 1065
- Design of four kinds of linear phase FIR filters -- complete set of Matlab source code
猜你喜欢

OPLG: 新一代云原生可观测最佳实践

Arcpy - - utilisation de la fonction insertlayer (): ajout de calques dans un document de carte

8、【STM32】定时器(TIM)——中断、PWM、输入捕获实验(一文精通定时器)

倍福EtherCAT Xml描述文件更新和下载

Dark horse notes - Common APIs

What features are added to Photoshop 2022 23.4.1? Do you know anything

Appearance mode (facade)

Processing function translate (mousex, mousey) learning

System tasks (display / print class) in Verilog - $display, $write, $strobe, $monitor

解中小企业之困,百度智能云打个样
随机推荐
【Spark】. Explanation of several icons of scala file in idea
Analysis of state transition diagram of Beifu NC axis
Electron official docs series: Testing And Debugging
Update and download of Beifu EtherCAT XML description file
Mysql database explanation (6)
5月产品升级观察站
Go structure method
P2393 yyy loves Maths II
Design of four kinds of linear phase FIR filters -- complete set of Matlab source code
Do you know the limitations of automated testing?
Electron official docs series: Best Practices
装饰器(Decorator)
What features are added to Photoshop 2022 23.4.1? Do you know anything
Electron official docs series: Testing And Debugging
解中小企业之困,百度智能云打个样
Electron official docs series: Distribution
倍福PLC实现绝对值编码器原点断电保持---bias的使用
Decorator
H - Sumsets POJ 2229
Processsing function random