sed命令
sed使用单引号,双引号有特殊的解释
sed是Stream Editor的缩写,流编辑器
sed是操作、过滤和转换文本内容的强大工具
常用功能包括结合正则表达式对文件实现快速增删改查,其中查询的功能中最常用的两大功能是过滤(过 滤指定字符串)、取行(取出指定行)
常用的参数选项:
1.-n:取消默认sed的输出,常与sed内置命令-p一起使用
2. -i:直接将修改结果写入文件,不用-i,sed修改的是内存数据
3. -e:多次编辑,不需要管道符
4. -r:支持正则扩展
sed的内置命令字符 用于对文件进行不同的操作功能,如对文件增删改查
sed常用 内置命令字符:范围 操作 内容
内容范围 w 文件名
sed的内置命令字符
a:append,对文本追加,在指定行后面添加一行/多行文本
d:Delete,删除匹配行
i:insert,标识插入文本,在指定行前添加一行/多行文本
p:Print,打印匹配行的内容,通常p与-n一起用
s/查找的内容/替换的内容/g: 匹配正则内容,然后替换内容(支持正则),结尾g代表全局匹配
c:替换指定行
w:筛选的内容保存到文件
r:读取文件并追加
sed匹配范围
1.空地址:全文处理
2.单地址:指定文件某一行
3.范围地址:10,20 十到二十行,10,+5 第10行向下5行,/pattern1/,/pattern2/
4./pattern/:包含//中的内容的行
5.步长:1~2,标识1、3、5、7、9奇数行,2~2两个步长,标识2、4、6、8、10偶数行
基本的正则列表

扩展正则列表

假设我们表中有五行数据

使用-n可以显示一行到三行的数据

如果只想显示带某个字母或者数字汉字的话可以使用过滤,这样可以只显示含有这个的字符串了

想要删除第几行到第几行的话可以使用d进行删除,想要删除带有L的行在下面第二个图(指定删除)


将文件中的Hello全部替换为His,如果想同时替换多个可以用-e来进行操作


在文件第二行追加内容a字符功能,写入到文件,还得添加-i,a后面就是添加的内容

添加多行信息,用换行符"\n"

在每一行下面插入新内容

在第二行上面插入内容
需要用到扩展正则时,需要加 -r选项 //把所有bash或nologin替换为xxxx

认识sed工具的p,d,s条件命令



下面看看sed工具的d指令案例(自己提前生成一个a.txt文件)


