sed使用场景:有时候需要修改配置文件或者脚本时,例如删除或置换某些关键词,就需要用到sed。
语法:
sed [选项] '地址定界和命令' file……
工作机制:每次读取一行文本至“模式空间(pattern space)”中,在模式空间中完成处理;将处理结果输出至标准输出设备;
选项:
-n: 静默模式,不显示没有匹配到的内容
-i:直接修改源文件
-e:script -e script:可以同时执行多个操作
-f:/path/to/sed_script :从指定文件中读取脚本并运行
-r:表示使用正则表达式
地址定界方式:
#:指定行
$:最后一行
/regexp/:任何能被正则表达式所匹配到的行
/regexp/|:匹配时忽略字符大小写
starLine(起始行),endLine(结束行)
#,/regexp/:从#行开始,到第一次被/regexp/所匹配到的行结束,中间的所有行;
#,#:#行开始,#行结束
/regexp1/,/regexp2/:从第一次被/regexp1/匹配到的行开始,到第一次被/regexp2/匹配到的行结束,中间的所有行;
#,+n:从#行开始,一直到向下的n行;
first~step:指定起始行,以及步长;
命令:
d:删除符合条件的行
=:显示行号;
p:显示符合条件的行
c \test:用text替换匹配到的行
a:\test:在指定的行后面追加新行,内容为test
i:\test:在指定的行前面添加新行,内容为test
r file:将指定的文件的内容添加至符合条件的行处
w file:将地址指定的范围内的行另存至指定的文件中
s/pattern/string/: 查找并替换,默认只替换每行中第一次被模式匹配到的字符串
g:全局替换
i:忽略字符大小写
s/// s### s@@@ 如果查找的内容出现和分割符一致可以更换分隔符,只要三个分隔符一样就可以
操作:
演示的文件:下面的一切操作都是根据此文件进行操作。
[root@localhost ~]# cat fstab # /etc/fstab # Created by anaconda on Thu Apr 23 09:07:40 2015 # # Accessible filesystems, by reference, aremaintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info # /dev/mapper/vg0-root / ext4 defaults 1 1 UUID=b48ca9ef-5675-459f-a148-25cbda0181db/boot ext4 defaults 1 2 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 /dev/mapper/vg0-var /var ext4 defaults 1 2 /dev/mapper/vg0-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
d:删除符合条件的行
删除1行到第3行
[root@localhost ~]# sed '1,3d' fstab # Accessible filesystems, by reference, aremaintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/orblkid(8) for more info # /dev/mapper/vg0-root / ext4 defaults 1 1 UUID=b48ca9ef-5675-459f-a148-25cbda0181db/boot ext4 defaults 1 2 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 /dev/mapper/vg0-var /var ext4 defaults 1 2 /dev/mapper/vg0-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
删除最后一行
[root@localhost ~]# sed '$d' fstab # /etc/fstab # Created by anaconda on Thu Apr 23 09:07:40 2015 # # Accessible filesystems, by reference, aremaintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info # /dev/mapper/vg0-root / ext4 defaults 1 1 UUID=b48ca9ef-5675-459f-a148-25cbda0181db/boot ext4 defaults 1 2 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 /dev/mapper/vg0-var /var ext4 defaults 1 2 /dev/mapper/vg0-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
删除包含default字符的行
[root@localhost ~]# sed '/default/d' fstab # /etc/fstab # Created by anaconda on Thu Apr 23 09:07:40 2015 # # Accessible filesystems, by reference, aremaintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info # devpts /dev/pts devpts gid=5,mode=620 0 0
=:显示行号
显示行号,比较坑的一点就是行号在行的上方,如果想在行前方可以使用cat -n
[root@localhost ~]# sed '=' fstab 1 # /etc/fstab 2 # Created by anaconda on Thu Apr 23 09:07:40 2015 3 # 4 # Accessible filesystems, by reference, aremaintained under '/dev/disk' 5 # See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info 6 # 7 /dev/mapper/vg0-root / ext4 defaults 1 1 8 UUID=b48ca9ef-5675-459f-a148-25cbda0181db/boot ext4 defaults 1 2 9 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 10 /dev/mapper/vg0-var /var ext4 defaults 1 2 11 /dev/mapper/vg0-swap swap swap defaults 0 0 12 tmpfs /dev/shm tmpfs defaults 0 0 13 devpts /dev/pts devpts gid=5,mode=620 0 0 14 sysfs /sys sysfs defaults 0 0 15 proc /proc proc defaults 0 0
只给#号开头的行显示行号
[root@localhost ~]# sed '/^#/=' fstab 1 # /etc/fstab 2 # Created by anaconda on Thu Apr 23 09:07:40 2015 3 # 4 # Accessible filesystems, by reference, aremaintained under '/dev/disk' 5 # See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info 6 # /dev/mapper/vg0-root / ext4 defaults 1 1 UUID=b48ca9ef-5675-459f-a148-25cbda0181db/boot ext4 defaults 1 2 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 /dev/mapper/vg0-var /var ext4 defaults 1 2 /dev/mapper/vg0-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
p:显示符合条件的行
显示最后一行;但是有一个缺点就是不去重,所以最后一行显示了两次
[root@localhost ~]# sed '$p' fstab # /etc/fstab # Created by anaconda on Thu Apr 23 09:07:40 2015 # # Accessible filesystems, by reference, aremaintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info # /dev/mapper/vg0-root / ext4 defaults 1 1 UUID=b48ca9ef-5675-459f-a148-25cbda0181db/boot ext4 defaults 1 2 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 /dev/mapper/vg0-var /var ext4 defaults 1 2 /dev/mapper/vg0-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 proc /proc proc defaults 0 0
[root@localhost ~]# sed -n'$p' fstab UUID=1a829b6d-763f-44cd-8544-f5fc4a480d05 swap swap defaults 0 0
c \test:用text替换匹配到的行
地址定界替换和模式匹配替换的区别:
地址定界:所有行替换为一行
模式匹配:所有行分别替换
地址定界:将第5行,到第7行替换为new text
[root@localhost ~]# sed -n '5,7c \new test' fstab new test
模式匹配:将第5行,到第7行分别替换为new text
[root@localhost ~]# sed '/^#/c \new test' fstab new test new test new test new test new test new test /dev/mapper/vg0-root / ext4 defaults 1 1 UUID=b48ca9ef-5675-459f-a148-25cbda0181db/boot ext4 defaults 1 2 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 /dev/mapper/vg0-var /var ext4 defaults 1 2 /dev/mapper/vg0-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
a:\test:在指定的行后面追加新行,内容为test
在第一行后面添加新行
[root@localhost ~]# sed '1a \新添加的行' fstab # /etc/fstab 新添加的行 # Created by anaconda on Thu Apr 23 09:07:40 2015 # # Accessible filesystems, by reference, aremaintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info # /dev/mapper/vg0-root / ext4 defaults 1 1 UUID=b48ca9ef-5675-459f-a148-25cbda0181db/boot ext4 defaults 1 2 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 /dev/mapper/vg0-var /var ext4 defaults 1 2 /dev/mapper/vg0-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
添加多行,在行尾处使用\n换号即可
[root@localhost ~]# sed '1a \添加的第一行\n第二行\n第三行' fstab # /etc/fstab 添加的第一行 第二行 第三行 # Created by anaconda on Thu Apr 23 09:07:40 2015 # # Accessible filesystems, by reference, aremaintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info # /dev/mapper/vg0-root / ext4 defaults 1 1 UUID=b48ca9ef-5675-459f-a148-25cbda0181db/boot ext4 defaults 1 2 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 /dev/mapper/vg0-var /var ext4 defaults 1 2 /dev/mapper/vg0-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
i:\test:在指定的行前面添加新行,内容为test
[root@localhost ~]# sed '1i \新添加的行' fstab 新添加的行 # /etc/fstab # Created by anaconda on Thu Apr 23 09:07:40 2015 # # Accessible filesystems, by reference, aremaintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info # /dev/mapper/vg0-root / ext4 defaults 1 1 UUID=b48ca9ef-5675-459f-a148-25cbda0181db /boot ext4 defaults 1 2 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 /dev/mapper/vg0-var /var ext4 defaults 1 2 /dev/mapper/vg0-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
r file:将指定的文件的内容添加至符合条件的行处
指定的文件
[root@localhost ~]# cat /etc/redhat-release CentOS release 6.6 (Final)
把指定的文件添加到第1行
[root@localhost ~]# sed '1r' /etc/redhat-releasefstab CentOS release 6.6 (Final) # /etc/fstab # Created by anaconda on Thu Apr 23 09:07:40 2015 # # Accessible filesystems, by reference, aremaintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info # /dev/mapper/vg0-root / ext4 defaults 1 1 UUID=b48ca9ef-5675-459f-a148-25cbda0181db/boot ext4 defaults 1 2 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 /dev/mapper/vg0-var /var ext4 defaults 1 2 /dev/mapper/vg0-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
w file:将地址指定的范围内的行另存至指定的文件中
将第一行到第五行,保存到test.txt文件中
[root@localhost ~]# sed '1,5w test.txt' fstab # /etc/fstab # Created by anaconda on Thu Apr 23 09:07:40 2015 # # Accessible filesystems, by reference, aremaintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info # /dev/mapper/vg0-root / ext4 defaults 1 1 UUID=b48ca9ef-5675-459f-a148-25cbda0181db/boot ext4 defaults 1 2 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 /dev/mapper/vg0-var /var ext4 defaults 1 2 /dev/mapper/vg0-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
查看保存的文件
[root@localhost ~]# cat test.txt # /etc/fstab # Created by anaconda on Thu Apr 23 09:07:40 2015 # # Accessible filesystems, by reference, are maintainedunder '/dev/disk' # See man pages fstab(5), findfs(8), mount(8)and/or blkid(8) for more info
删除行首开头的#号
[root@localhost ~]# sed 's@^#@@' fstab /etc/fstab Created byanaconda on Thu Apr 23 09:07:40 2015 Accessiblefilesystems, by reference, are maintained under '/dev/disk' See manpages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info /dev/mapper/vg0-root / ext4 defaults 1 1 UUID=b48ca9ef-5675-459f-a148-25cbda0181db/boot ext4 defaults 1 2 /dev/mapper/vg0-usr /usr ext4 defaults 1 2 /dev/mapper/vg0-var /var ext4 defaults 1 2 /dev/mapper/vg0-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
练习:
(1) 删除/boot/grub/grub.conf文件中所有行的行首的空白字符;
sed 's/^[[:space:]]\+//' /boot/grub/grub.conf
(2) 删除/etc/fstab文件中所有以#开头,后跟至少一个空白字符的行的行首的#和空白字符;
sed 's/^#[[:space:]]\+//' /etc/fstab
(3) 把/etc/fstab文件的奇数行另存为/tmp/fstab.3;
sed '1~2w /tmp/fstab.3' /etc/fstab
(4) echo一个文件路径给sed命令,取出其基名;进一步地,取出其路径名;
取基名:echo "/etc/sysconfig/network-scripts/" | sed 's@^.*/\([^/]\+\)/\?$@\1@'
取路径名:echo "/etc/sysconfig/network-scripts/" | sed 's@[^/]\+/\?$@@'