Linux中的命令主要是操作文件或字符串,对字符串进行匹配、修改、替换是非常常见的操作。sed作为Shell三剑客之一,对文件中的字符串处理尤其擅长,不仅可以将修改后的结果在标准输出显示,还可以直接替换修改原始文件。
sed的工作流程
- sed把当前正在处理的行保存在一个临时缓冲区中(也称为模式空间),然后处理临时缓冲区中的行,完成后把该行发送到屏幕上。
- sed把每一行都存在临时缓冲区中,对这个副本进行编辑,所以不会修改原文件。
- sed主要用来自动编辑一个或多个文件;简化对文件的反复操作、编写转换程序等。
语法格式
sed [-nefri] 'command' 输入文本
常用选项:
-e 进行多项编辑,即对输入行应用多条sed命令时使用
-n 取消默认的输出
-f 指定sed脚本的文件名
-r 使用扩展正则表达式
-i inplace,原地编辑(修改源文件)
命令选项:
p 打印匹配行
= 显示文件行号
a/ 在定位行号后附加新文本信息
i/ 在定位行号后插入新文本信息
d 删除定位行
c/ 用新文本替换定位文本
s 使用替换模式替换相应模式
r 从另一个文本中读文本
w 写文本到一个文件
sed -n '1,5p' input.txt 打印1-5行
sed -n '$p' input.txt 打印最后一行
sed -n '/root/p' input.txt 打印包括root的整行
sed '1,5d' input.txt 删除1-5行
sed '/root/d' input.txt 删除包括root的行
!取反所选行:
hioier@yunpc:~/scripts$ sed -n '1,3!p' input.txt
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
行首添加注释
行首行尾同时替换:
sed -n '1,3s/^/# &/g;1,3s/$/& >>/gp' input.txt
i\ 在当前行之前插入文本。多行时除最后一行外,每行末尾需用"\"续行
a\ 在当前行后添加一行或多行。多行时除最后一行外,每行末尾需用“\”续行
c\ 用此符号后的新文本替换当前行中的文本。多行时除最后一行外,每行末尾需用"\"续行,整行替换
hioier@yunpc:~/scripts$ sed '/root/a\hioier.com' input.txt
root:x:0:0:root:/root:/bin/bash
hioier.com
root:x:0:0:root:/root:/bin/bash
hioier.com
root:x:0:0:root:/root:/bin/bash
hioier.com
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
hioier@yunpc:~/scripts$ sed '/root/a\hioier.com\nhello cat.' input.txt
root:x:0:0:root:/root:/bin/bash
hioier.com
hello cat.
root:x:0:0:root:/root:/bin/bash
hioier.com
hello cat.
root:x:0:0:root:/root:/bin/bash
hioier.com
hello cat.
hioier@yunpc:~/scripts$ sed '/root/c\hioier.com\
> hello cat.
> ' input.txt
hioier.com
hello cat.
hioier.com
hello cat.
hioier.com
hello cat.
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
hioier@yunpc:~/scripts$ sed '2r test.txt' input.txt
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
hioier.com 1
hioier.com 2
hioier.com 3
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
hioier@yunpc:~/scripts$ sed '/root/w output.txt' input.txt
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
hioier@yunpc:~/scripts$ cat output.txt
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
hioier@yunpc:~/scripts$ sed -n '/root/=' input.txt
1
2
3
hioier@yunpc:~/scripts$ sed -ne '/root/=' -ne '/root/p' input.txt
1
root:x:0:0:root:/root:/bin/bash
2
root:x:0:0:root:/root:/bin/bash
3
root:x:0:0:root:/root:/bin/bash
hioier@yunpc:~/scripts$
sed脚本就是将多个操作命令放到一个文件中执行。
过滤.bashrc配置文件:
sed '/^#/d;/^$/d;/^\t$/d;/^\t#/d;/^.*#/d' bashrc
在文件收尾添加信息:
sed '1i\------ start ------' bashrc
sed '$a\------ end ------' bashrc
sed.sh
#!/bin/sed -f
s/root/hioier/g
1i\
------- start ------
$a\
------- end ------
sed -f sed.sh input.txt
./sed.sh -i input.txt
相关推荐
- Linux Shell三剑客之awk,功能强大的文本处理工具
- 什么是Linux系统?我们是否要用Linux
- Linux革命性工具,shell脚本自动化
- Linux神之编辑器VIM,史上最强编辑器!
- 什么是C 变量,就是存储数据的盒子
- 什么是计算机网络?一文带你了解计算机网络发展
- 一文带你了解计算机网络体架构,OSI模型和TCP/IP模型