sed 行编辑器,Stream EDitor
是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”,接着用sed命令处理缓冲区中的
内容,处理完成后,把缓冲区的内容送往屏幕。然后读入下行,执行下一个循环。如果没有使诸如‘D’的特殊命令,那会在两个循环之间清空模
式空间,但不会清空保留空间。这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。
命令用法:
sed [option]... 'script' inputfile...
选项:
-n 不输出模式空间内容到屏幕,即不自动打印
-e 多点编辑
-f /PATH/SCRIPT_FILE 从指定文件中读取编辑脚本
-r 支持使用扩展正则表达式
-i.bak 备份文件并原处编辑
' ' 里面的内容包含 地址+命令
sed ' ' passwd 默认会将匹配到的行打印显示到屏幕上,如果需要输出特定某一行需要加 -n ,例如;
[root@centos7 data]# sed -n '2p' passwd
bin:x:1:1:bin:/bin:/sbin/nologin
sed 配和正则表达式,必须要加 / /
[root@centos7 data]#ifconfig ens33|sed -n '/netmask/p'
inet 192.168.48.106 netmask 255.255.255.0 broadcast 192.168.48.255
地址界定:
(1) 不给地址:对全文进行处理
(2) 单地址:
#:指定的行,$:最后一行
/pattern/:被此处模式所能够匹配到的每一行
(3) 地址范围:
#,#
#,+#
/pat1/,/pat2/
#,/pat1/
(4) ~:步进
1~2 奇数行 起始行数~步长
2~2 偶数行 起始行数~步长
面试经常问的题目,查看某一段时间的日志
[root@centos7 data]#sed -n '/^b/,/^f/p' passwd
[root@centos7 data]#sed '/^#/d;/^$/d' /etc/fstab 使用;可以进行两个操作
a [\]text 在指定行后面追加文本,支持使用\n实现多行追加
例如:
sed -n '1~2a line' passwd
编辑命令:
d 删除模式空间匹配的行,并立即启用下一轮循环
p 打印当前模式空间内容,追加到默认输出之后
a [\]text 在指定行后面追加文本,支持使用\n实现多行追加
i [\]text 在行前面插入文本
[root@centos7 data]#sed '/^Listen/i listen 8080' /etc/httpd/conf/httpd.conf|
c [\]text 替换行为单行或多行文本
sed '/^SELINUX=/c SELINUX=enforcing' /etc/selinux/config #enforcing 为修改期望值
w /path/file 保存模式匹配的行至指定文件
sed '/^#/d;w /data/httpd.conf' /etc/httpd/conf/httpd.conf #相当于2个命令,用;隔开
r /path/file 读取指定文件的文本至模式空间中匹配到的行后
[root@centos7 data]#seq 10 |sed '2~2r /etc/issue'
1
2
\S
Kernel \r on an \m
= 为模式空间中的行打印行号
! 模式空间中匹配行取反处理
[root@centos7 data]# seq 10 | sed -n '1~2!p'
2
4
6
8
10
sed工具
s/// 查找替换,支持使用其它分隔符,s@@@,s###
替换标记:
g 行内全局替换
p 显示替换成功的行
w /PATH/FILE 将替换成功的行保存至文件中
sed用法示例;
sed ‘2p’ /etc/passwd
sed -n ‘2p’ /etc/passwd
sed -n ‘1,4p’ /etc/passwd
sed -n ‘/root/p’ /etc/passwd
sed -n ‘2,/root/p’ /etc/passwd 从2行开始
sed -n ‘/^$/=’ file 显示空行行号
sed -n -e ‘/^$/p’ -e ‘/^$/=’ file
sed‘/root/a\superman’ /etc/passwd行后
sed ‘/root/i\superman’ /etc/passwd 行前
sed ‘/root/c\superman’ /etc/passwd 代替行
sed ‘/^$/d’ file
sed ‘1,10d’ file
nl /etc/passwd | sed ‘2,5d’
nl /etc/passwd | sed ‘2a tea’
sed 's/test/mytest/g' example
sed –n ‘s/root/&superman/p’ /etc/passwd 单词后
sed –n ‘s/root/superman&/p’ /etc/passwd 单词前
sed -e ‘s/dog/cat/’ -e ‘s/hi/lo/’ pets
sed –i.bak ‘s/dog/cat/g’ pets
centos 6的环境:
[root@localhost ~]#sed -n '/#<VirtualHost/,/#<\/VirtualHost/p' /etc/httpd/conf/httpd.conf
#<VirtualHost *:80>
# ServerAdmin webmaster@dummy-host.example.com
# DocumentRoot /www/docs/dummy-host.example.com
# ServerName dummy-host.example.com
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
#</VirtualHost>
在上面基础上,将#去掉
[root@localhost ~]#sed -n '/#<VirtualHost/,/#<\/VirtualHost/s@#@@p' /etc/httpd/conf/httpd.conf
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /www/docs/dummy-host.example.com
ServerName dummy-host.example.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
取文件夹或者基目录;
[root@localhost ~]#echo /etc/sysconfig/network-scripts/ |sed -r 's@(^/.*/)([^/]+)/?$@\1@'
/etc/sysconfig/
[root@localhost ~]#echo /etc/sysconfig/network-scripts/ |sed -r 's@(^/.*/)([^/]+)/?$@\2@'
network-scripts
将/etc/sysconfig/和network-scripts 进行分组,以/ 开头的是第一个分组,非/ 后续的为第二组,进行取值
取出如下文件中,linux16的行
[root@centos7 etc]#sed -rn '/^[[:space:]]+linux16/p' /boot/grub2/grub.cfg
linux16 /vmlinuz-3.10.0-957.el7.x86_64 root=UUID=950190f0-76b6-4783-9694-4ffb7680948d ro crashkernel=auto rhgb quiet LANG=en_US.UTF-8
linux16 /vmlinuz-0-rescue-111987d8e51743fab3e3544fa325e2e1 root=UUID=950190f0-76b6-4783-9694-4ffb7680948d ro crashkernel=auto rhgb quiet
**********将上面取出来的所有行匹配,进行分组,在后面追加 net.infname=0#p,这里比较难理解
[root@centos7 etc]#sed -rn '/^[[:space:]]+linux16/s#(.*)#\1 net.infname=0#p' /boot/grub2/grub.cfg
linux16 /vmlinuz-3.10.0-957.el7.x86_64 root=UUID=950190f0-76b6-4783-9694-4ffb7680948d ro crashkernel=auto rhgb quiet LANG=en_US.UTF-8 net.infname=0
linux16 /vmlinuz-0-rescue-111987d8e51743fab3e3544fa325e2e1 root=UUID=950190f0-76b6-4783-9694-4ffb7680948d ro crashkernel=auto rhgb quiet net.infname=0
& 与上面分组用法\1 是一样的效果
[root@centos7 etc]#sed -rn '/^[[:space:]]+linux16/s#(.*)#& net.infname=0#p' /boot/grub2/grub.cfg
linux16 /vmlinuz-3.10.0-957.el7.x86_64 root=UUID=950190f0-76b6-4783-9694-4ffb7680948d ro crashkernel=auto rhgb quiet LANG=en_US.UTF-8 net.infname=0
linux16 /vmlinuz-0-rescue-111987d8e51743fab3e3544fa325e2e1 root=UUID=950190f0-76b6-4783-9694-4ffb7680948d ro crashkernel=auto rhgb quiet net.infname=0
将变量放入单引号里面,特殊比较少见
只是举个例子,我执行也没有生效
sed 's/'''$var''' /ttyang/' passwd
sed的高级用法
模式空间 -->保持空间
先读入到模式空间,对于模式空间处理的不够,暂时加入保持空间
各个参数含义:
P: 打印模式空间开端至\n内容,并追加到默认输出之前
h: 把模式空间中的内容覆盖至保持空间中
H:把模式空间中的内容追加至保持空间中
g: 从保持空间取出数据覆盖至模式空间
G:从保持空间取出内容追加至模式空间
x: 把模式空间中的内容与保持空间中的内容进行互换
n: 读取匹配到的行的下一行覆盖至模式空间
N:读取匹配到的行的下一行追加至模式空间
d: 删除模式空间中的行
D:如果模式空间包含换行符,则删除直到第一个换行符的模式空间中的文本,
并不会读取新的输入行,而使用合成的模式空间重新启动循环。如果模式空间
不包含换行符,则会像发出d命令那样启动正常的新循环
例子:
1、[root@centos7 ~]#seq 10 |sed -n 'n;p' #取出偶数行
2
4
6
8
10
2、[root@centos7 ~]#seq 10 |sed '1!G;h;$!d' #倒序显示
10
9
8
7
6
5
4
3
2
1
3、sed‘N;D’FILE
4、sed '$!N;$!D' FILE
5、sed '$!d' FILE
6、sed ‘G’ FILE
7、sed ‘g’ FILE
8、sed ‘/^$/d;G’ FILE
9、sed 'n;d' FILE
10、sed -n '1!G;h;$p' FILE