Linux文本三剑客之sed
1. sed 脚本语言
- sed即Stream EDitor,sed自己的脚本语言,与vi不同,sed是编辑器
脚本 '地址' + '命令' 组成
脚本 '命令' #没有地址就是全文
常用选项
-n:关闭自动打印
-r:使用扩展正则表达式
脚本命令
p:打印
q:退出
d:删除
a:在前面插入内容
i:在后面插入内容
c:替换内容
w file:保存匹配行到指定文件
r file:读取指定文件的文本至模式空间中匹配的行后
=:匹配行取反
!:打印行号
[root@localhost ~]#sed --help
用法: sed [选项]... {sed自己的脚本} [输入文件]...
[root@localhost ~]#sed ' ' /etc/resolv.conf #查看文件内容
# Generated by NetworkManager
nameserver 8.8.8.8
xargs #接收标准输出,给后面的命令,做参数
对于不接受管道符的命令可以用xargs作为中间转换
xargs echo
[root@localhost ~]#xargs #ctrl+D传参并退出
lll
lll
[root@localhost ~]#xargs echo #ctrl+D传参并退出
lll
lll
[root@localhost ~]#seq 10
1
2
3
4
5
6
7
8
9
10
[root@localhost ~]#seq 10 | xargs
1 2 3 4 5 6 7 8 9 10
[root@localhost ~]#seq 10 | xargs -n1 #-n1一次传一个
1
2
3
4
5
6
7
8
9
10
[root@localhost ~]#xargs ls
/
bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@localhost ~]#xargs ls
/root
anaconda-ks.cfg expect test.txt 公共 视频 文档 音乐
a.txt initial-setup-ks.cfg test.txt.bak 模板 图片 下载 桌面
[root@localhost ~]#ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.8.100 netmask 255.255.255.0 broadcast 192.168.8.255
inet6 fe80::dc8b:de58:8fff:ce65 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:76:f0:1a txqueuelen 1000 (Ethernet)
RX packets 176821 bytes 251371933 (239.7 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 20938 bytes 1305045 (1.2 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost ~]#ifconfig ens33 | sed -n '2p' #打印第二行
inet 192.168.8.100 netmask 255.255.255.0 broadcast 192.168.8.255
[root@localhost ~]#sed -n '/^root/p' /etc/passwd #以root开关的行
root:x:0:0:root:/root:/bin/bash
[root@localhost ~]#sed -n '2,/^g/p' /etc/passwd #从第二行开始到以g开头的行
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
[root@localhost ~]#ifconfig ens33 | sed '1q' #打印第一行
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
[root@localhost ~]#ifconfig ens33 | sed '3q' #打印第一行到第三行
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.8.100 netmask 255.255.255.0 broadcast 192.168.8.255
inet6 fe80::dc8b:de58:8fff:ce65 prefixlen 64 scopeid 0x20<link>
[root@localhost ~]#ifconfig ens33 | sed -n '3p' #打印第三行
inet6 fe80::dc8b:de58:8fff:ce65 prefixlen 64 scopeid 0x20<link>
[root@localhost ~]#ifconfig ens33 | sed -n '1,3p' #打印第一行到第三行
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.8.100 netmask 255.255.255.0 broadcast 192.168.8.255
inet6 fe80::dc8b:de58:8fff:ce65 prefixlen 64 scopeid 0x20<link>
[root@localhost ~]#ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.8.100 netmask 255.255.255.0 broadcast 192.168.8.255
inet6 fe80::dc8b:de58:8fff:ce65 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:76:f0:1a txqueuelen 1000 (Ethernet)
RX packets 177196 bytes 251398928 (239.7 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 21093 bytes 1321207 (1.2 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
#打印3-5行
[root@localhost ~]#ifconfig ens33 | sed -n '3,+2p'
inet6 fe80::dc8b:de58:8fff:ce65 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:76:f0:1a txqueuelen 1000 (Ethernet)
RX packets 177151 bytes 251395544 (239.7 MiB)
[root@localhost ~]#seq 10 | sed -n 'n;p' #打印偶数行 n将下一行留在高级空间
2
4
6
8
10
[root@localhost ~]#seq 10 | sed -n '2,${n;p}' #从2开始打印奇数行
3
5
7
9
[root@localhost ~]#seq 10 | sed '2d' #删除第二行
1
3
4
5
6
7
8
9
10
[root@localhost ~]#seq 10 | sed '0~2d' #0开始步长为2的删除
1
3
5
7
9
[root@localhost ~]#seq 10 | sed '1~2d' #1开始步长为2的删除
2
4
6
8
10
[root@localhost ~]#seq 10 | sed '3a#' #a表示在3后面插入内容
1
2
3
#
4
5
6
7
8
9
10
[root@localhost ~]#seq 10 | sed '3a@'
1
2
3
@
4
5
6
7
8
9
10
[root@localhost ~]#seq 10 | sed '3i*' #i表示在3前面插入内容
1
2
*
3
4
5
6
7
8
9
10
[root@localhost ~]#seq 10 | sed '3chello' #c表示替换3的内容
1
2
hello
4
5
6
7
8
9
10
[root@localhost ~]#seq 10 | sed '3a aa\nbb\ncc' #在3后面插入aa bb cc \n换行
1
2
3
aa
bb
cc
4
5
6
7
8
9
10
[root@localhost ~]#seq 10 | sed '3i aa\nbb\ncc' #在3前面插入aa bb cc \n换行
1
2
aa
bb
cc
3
4
5
6
7
8
9
10
[root@localhost ~]#seq 10 | sed '3c aa\nbb\ncc' #将3内容替换成aa bb cc \n换行
1
2
aa
bb
cc
4
5
6
7
8
9
10
[root@localhost ~]#seq 10 |sed -n '3wa.txt' #将3插入a.txt文件中
[root@localhost ~]#cat a.txt
3
[root@localhost ~]#seq 10 |sed -n '0~2wa.txt' #将偶数插入a.txt文件中
[root@localhost ~]#cat a.txt
2
4
6
8
10
[root@localhost ~]#seq 10 |sed -n '1~2wa.txt' #将奇数插入a.txt文件中
[root@localhost ~]#cat a.txt
1
3
5
7
9
[root@localhost ~]#cat /etc/issue #查看文件
\S
Kernel \r on an \m
[root@localhost ~]#seq 10 | sed '3r /etc/issue' #在3后面插入文件内容
1
2
3
\S
Kernel \r on an \m
4
5
6
7
8
9
10
[root@localhost ~]#sed '3=' /etc/issue #在第三行前插入行号
\S
Kernel \r on an \m
3
[root@localhost ~]#seq 10 |sed -n '1~2!p' #取反,打印出偶数
2
4
6
8
10
[root@localhost ~]#vim test.txt
[root@localhost ~]#cat test.txt
1
2
3
4
5
6
7
8
9
10
[root@localhost ~]#sed -i.bak '3ajkl' test.txt #在3后面插入内容
[root@localhost ~]#cat test.txt #查看
1
2
3
jkl
4
5
6
7
8
9
10
[root@localhost ~]#sed -i.bak '3ihjk' test.txt #在3前面插入内容
[root@localhost ~]#cat test.txt #查看
1
2
hjk
3
4
5
6
7
8
9
10
[root@localhost ~]#sed -i.bak '3chello' test.txt #将3替换成hello
[root@localhost ~]#cat test.txt
1
2
hello
4
5
6
7
8
9
10
[root@localhost ~]#seq 10 >test.txt #1-10追加到test.txt文档中
[root@localhost ~]#cat test.txt
1
2
3
4
5
6
7
8
9
10
[root@localhost ~]#sed -i.bak '4d' test.txt #删除第4行
[root@localhost ~]#ls
anaconda-ks.cfg initial-setup-ks.cfg test.txt.bak 模板 图片 下载 桌面
expect test.txt 公共 视频 文档 音乐
[root@localhost ~]#cat test.txt #查看文档,第4行已经删除
1
2
3
5
6
7
8
9
10
[root@localhost ~]#cat test.txt.bak
1
2
3
4
5
6
7
8
9
10
[root@localhost ~]#seq 10 | sed 'p' #p带有自动打印功能,又打印一遍内容
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
2. 搜索替换
[root@localhost ~]#cat /etc/selinux/config #搜索替代
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
[root@localhost ~]#sed 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
[root@localhost ~]#rpm -q httpd #查看是否已安装httpd
httpd-2.4.6-99.el7.centos.1.x86_64
[root@localhost ~]#sed -i 's/Listen 80/Listen 8080/' /etc/httpd/conf/httpd.conf #搜索替换
[root@localhost ~]#grep 80 /etc/httpd/conf/httpd.conf
#Listen 12.34.56.78:80
Listen 8080
#ServerName www.example.com:80
[root@localhost ~]#sed -n '/^r/,/^d/p' /etc/passwd #/etc/passwd中以r开关,以d结尾的行
[root@localhost ~]#grep 'r..t' /etc/passwd #过滤以r开头,t为结尾的行
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[root@localhost ~]#sed -i 's/r..t/&er/g' /etc/passwd #在r开头,t结尾的字段加er
[root@localhost ~]#cat /etc/passwd
rooter:x:0:0:rooter:/rooter:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/rooter:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/fterp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
3. 分组
[root@localhost ~]#echo 123abcdef | grep -E '[0-9]+'
123abcdef
[root@localhost ~]#echo 123abcdef | awk -Fa '{print $1}'
123
[root@localhost ~]#echo 123abcdef | sed -rn 's/(123)(abc)(def)/\1/p'
123
[root@localhost ~]#echo 123abcdef | sed -rn 's/(123)(abc)(def)/\3\2\1/p'
defabc123
[root@localhost ~]#echo 123abcdef | sed -rn 's/(123)(abc)(def)/\2\3\1/p'
abcdef123
[root@localhost ~]#echo 123abcdef | sed -rn 's/(123)(abc)(def)/\2\1\3/p'
abc123def
[root@localhost ~]#echo "/etc/sysconfig/network-scripts/ifcfg-ens33" | sed -nr 's@(.*/)(.*)@\2@p'
ifcfg-ens33
[root@localhost ~]#echo "/etc/sysconfig/network-scripts/ifcfg-ens33" | sed -nr 's@(.*/)(.*)@\1@p'
/etc/sysconfig/network-scripts/
[root@localhost ~]#echo "/etc/sysconfig/network-scripts/ifcfg-ens33" | sed -nr 's@(.*/)(.*)@\0@p'
/etc/sysconfig/network-scripts/ifcfg-ens33