sed基本用法:
sed: Stream EDitor
行编辑器 (全屏编辑器: vi)
sed: 模式空间
默认不编辑原文件,仅对模式空间中的数据做处理;而后,处理结束后,将模式空间打印至屏幕;
sed [options] ‘AddressCommand‘ file ...
-n: 静默模式,不再默认显示模式空间中的内容
-i: 直接修改原文件
-e SCRIPT -e SCRIPT:可以同时执行多个脚本
-f /PATH/TO/SED_SCRIPT
sed -f /path/to/scripts file
-r: 表示使用扩展正则表达式
Address:
1、StartLine,EndLine
比如1,100
$:最后一行
2、/RegExp/
/^root/
3、/pattern1/,/pattern2/
第一次被pattern1匹配到的行开始,至第一次被pattern2匹配到的行结束,这中间的所有行
4、LineNumber
指定的行
5、StartLine, +N
从startLine开始,向后的N行;
Command:
d: 删除符合条件的行;
p: 显示符合条件的行;
a \string: 在指定的行后面追加新行,内容为string
\n:可以用于换行
[[email protected] ~]# sed ‘1a \###\nfushanhua‘ /etc/fstab
/dev/mapper/VolGroup-lv_root / ext4 defaults 1 1
###
fushanhua
UUID=a502aad2-4118-48be-bf98-f1660bab1c1e /boot ext4 defaults 1 2
/dev/mapper/VolGroup-lv_swap swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
[[email protected] ~]# sed ‘1,3a \###\nfushanhua‘ /etc/fstab
/dev/mapper/VolGroup-lv_root / ext4 defaults 1 1
###
fushanhua
UUID=a502aad2-4118-48be-bf98-f1660bab1c1e /boot ext4 defaults 1 2
###
fushanhua
/dev/mapper/VolGroup-lv_swap swap swap defaults 0 0
###
fushanhua
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
i \string: 在指定的行前面添加新行,内容为string
r FILE: 将指定的文件的内容添加至符合条件的行处
[[email protected] ~]# sed ‘1r /etc/issue‘ /etc/fstab
/dev/mapper/VolGroup-lv_root / ext4 defaults 1 1
CentOS release 6.7 (Final)
Kernel \r on an \m
UUID=a502aad2-4118-48be-bf98-f1660bab1c1e /boot ext4 defaults 1 2
/dev/mapper/VolGroup-lv_swap swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
[[email protected] ~]# sed ‘1,3r /etc/issue‘ /etc/fstab
/dev/mapper/VolGroup-lv_root / ext4 defaults 1 1
CentOS release 6.7 (Final)
Kernel \r on an \m
UUID=a502aad2-4118-48be-bf98-f1660bab1c1e /boot ext4 defaults 1 2
CentOS release 6.7 (Final)
Kernel \r on an \m
/dev/mapper/VolGroup-lv_swap swap swap defaults 0 0
CentOS release 6.7 (Final)
Kernel \r on an \m
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
[[email protected] ~]# sed ‘$r /etc/issue‘ /etc/fstab
/dev/mapper/VolGroup-lv_root / ext4 defaults 1 1
UUID=a502aad2-4118-48be-bf98-f1660bab1c1e /boot ext4 defaults 1 2
/dev/mapper/VolGroup-lv_swap swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
CentOS release 6.7 (Final)
Kernel \r on an \m
w FILE: 将地址指定的范围内的行另存至指定的文件中;
[[email protected] ~]# sed ‘$w /tmp/zui.txt‘ /etc/fstab
/dev/mapper/VolGroup-lv_root / ext4 defaults 1 1
UUID=a502aad2-4118-48be-bf98-f1660bab1c1e /boot ext4 defaults 1 2
/dev/mapper/VolGroup-lv_swap swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
[[email protected] ~]# cat /tmp/zui.txt
proc /proc proc defaults 0 0
s/pattern/string/修饰符: 查找并替换,默认只替换每行中第一次被模式匹配到的字符串
加修饰符
g: 全局替换
i: 忽略字符大小写
s///: s###, [email protected]@@
\(\), \1, \2
l..e: like-->liker
love-->lover
like-->Like
love-->Love
&: 引用模式匹配整个串
[[email protected] tmp]# sed ‘s#l..e#&r#g‘ zui.txt
hello ,liker
hi,my lover
[[email protected] tmp]# sed ‘s#\(l..e\)#\1r#g‘ zui.txt
hello ,liker
hi,my lover
[[email protected] tmp]# sed ‘s#\(l..e\)#\1\n#g‘ zui.txt
hello ,like
hi,my love
[[email protected] tmp]# sed ‘s#\(l..e\)#\1.#g‘ zui.txt
hello ,like.
hi,my love.
[[email protected] tmp]# sed ‘s#l\(..e\)#L\1#g‘ zui.txt
hello ,Like
hi,my Love
sed练习:
1、删除/etc/grub.conf文件中行首的空白符;
sed -r ‘[email protected]^[[:spapce:]][email protected]@g‘ /etc/grub.conf
2、替换/etc/inittab文件中"id:3:initdefault:"一行中的数字为5;
sed ‘[email protected]\(id:\)[0-9]\(:initdefault:\)@\15\[email protected]‘ /etc/inittab
3、删除/etc/inittab文件中的空白行;
sed ‘/^$/d‘ /etc/inittab
4、删除/etc/inittab文件中开头的#号;
sed ‘[email protected]^#@@g‘ /etc/inittab
5、删除某文件中开头的#号及后面的空白字符,但要求#号后面必须有空白字符;
sed -r ‘[email protected]^#[[:space:]][email protected]@g‘ /etc/inittab
6、删除某文件中以空白字符后面跟#类的行中的开头的空白字符及#
sed -r ‘[email protected]^[[:space:]]+#@@g‘ /etc/inittab
7、取出一个文件路径的目录名称;
echo "/etc/rc.d/" | sed -r ‘[email protected]^(/.*/)[^/]+/[email protected]\[email protected]‘
基名:
echo "/etc/rc.d/" | sed -r ‘[email protected]^/.*/([^/]+)/[email protected]\[email protected]‘
#abc
# hello world
# hi world
练习:
传递一个用户名参数给脚本,判断此用户的用户名跟其基本组的组名是否一致,并将结果显示出来。
练习例子:(/etc/fstab)
Address:
1、StartLine,EndLine
比如1,100
$:最后一行
[[email protected] ~]# sed -n 1,3p /etc/fstab
/dev/mapper/VolGroup-lv_root / ext4 defaults 1 1
UUID=a502aad2-4118-48be-bf98-f1660bab1c1e /boot ext4 defaults 1 2
/dev/mapper/VolGroup-lv_swap swap swap defaults 0 0
[[email protected] ~]# sed -n 1,3d /etc/fstab
[[email protected] ~]# sed 1,3d /etc/fstab
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
2、/RegExp/
/^root/
[[email protected] ~]# sed -r -n ‘/^#|^$/p‘ /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue May 31 07:30:11 2016
#
# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
3、/pattern1/,/pattern2/
第一次被pattern1匹配到的行开始,至第一次被pattern2匹配到的行结束,这中间的所有行
[[email protected] ~]# sed -n ‘/devpts/,/proc/p‘ /etc/fstab
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
[[email protected] ~]# sed ‘/devpts/,/proc/d‘ /etc/fstab
/dev/mapper/VolGroup-lv_root / ext4 defaults 1 1
UUID=a502aad2-4118-48be-bf98-f1660bab1c1e /boot ext4 defaults 1 2
/dev/mapper/VolGroup-lv_swap swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
4、LineNumber
指定的行
[[email protected] ~]# sed -n ‘$p‘ /etc/fstab
proc /proc proc defaults 0 0
[[email protected] ~]# sed -n ‘5p‘ /etc/fstab
devpts /dev/pts devpts gid=5,mode=620 0 0
5、StartLine, +N
从startLine开始,向后的N行;
[[email protected] ~]# sed ‘2,+2p‘ /etc/fstab
/dev/mapper/VolGroup-lv_root / ext4 defaults 1 1
UUID=a502aad2-4118-48be-bf98-f1660bab1c1e /boot ext4 defaults 1 2
UUID=a502aad2-4118-48be-bf98-f1660bab1c1e /boot ext4 defaults 1 2
/dev/mapper/VolGroup-lv_swap swap swap defaults 0 0
/dev/mapper/VolGroup-lv_swap swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc
经典案例:
sed 取接口IP地址:
[[email protected] ~ 18:50:47]$ ifconfig eth0
eth0 Link encap:Ethernet HWaddr 90:B1:1C:47:A7:E8
inet addr:192.168.0.35 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::92b1:1cff:fe47:a7e8/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1496 Metric:1
RX packets:5096955365 errors:0 dropped:104 overruns:0 frame:0
TX packets:5128482803 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2361903276011 (2.1 TiB) TX bytes:2114718154002 (1.9 TiB)
Interrupt:35
[[email protected] ~ 18:49:28]$ ifconfig eth0|sed -r -n ‘s#^.*addr:(.*) Bca.*$#\1#g‘
[[email protected] ~ 18:49:54]$ ifconfig eth0|sed -r -n ‘s#^.*addr:(.*) Bca.*$#\1#gp‘
192.168.0.35
[[email protected] ~ 18:49:59]$ ifconfig eth0|sed -r -n ‘s#^.*addr:(.*) Bca.*$#\1#g‘p
192.168.0.35