sed是一款流编辑器工具,通常我们用来对文本进行过滤与替换操作,特别是当你想要对几十个配置文件做统一更改时,你会感受到sed的魅力。它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。sed的工作流程如下图所示:
sed:Stream EDitor,行编辑器
用法: sed [选项]… {脚本(如果没有其他脚本)} [输入文件]…
常用选项:
-e:多点编辑
-f /PATH/TO/SCRIPT_FILE:从指定文件中读取编辑脚本:
-r:支持使用扩展正则表达式
-n:不输出模式中的内容至屏幕
-i: 原处编辑
!:取反条件
s/查找的条件/替换的内容/:
s@@@ s###
编辑命令:
a,append表示追加指令
i,insert表示插入指令
d,delete表示删除指令
s,substitution表示替换指令
p,显示模式空间中的内容
w /path/to/somefile:保存模式空间中的内容到指定文件中
r /path/to/somefile: 读取指定文件的文件流至模式空间中的行(能匹配到的行)
=:为模式空间中的行打印行号
关于d删除的用法:
[root@C67-X64-A1 ~]# cat /etc/fstab # # /etc/fstab # Created by anaconda on Tue Jul 5 23:40:37 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 # UUID=922eb46f-7e6e-4670-8bf1-6f9f1b05a053 / ext4 defaults 1 1 UUID=3881dbb7-5500-4eff-ba82-17651d436ce5 /boot ext4 defaults 1 2 UUID=419bb1cd-93b2-403b-9bf2-847d3072248b /tmp ext4 defaults 1 2 UUID=bcb87417-3008-4fcc-80d7-41c3ef9ff96c 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
# 删除以UUID开头的行:
[root@C67-X64-A1 ~]# sed '/^UUID/d' /etc/fstab # # /etc/fstab # Created by anaconda on Tue Jul 5 23:40:37 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 # 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@C67-X64-A1 ~]# sed '/^#/d' /etc/fstab UUID=922eb46f-7e6e-4670-8bf1-6f9f1b05a053 / ext4 defaults 1 1 UUID=3881dbb7-5500-4eff-ba82-17651d436ce5 /boot ext4 defaults 1 2 UUID=419bb1cd-93b2-403b-9bf2-847d3072248b /tmp ext4 defaults 1 2 UUID=bcb87417-3008-4fcc-80d7-41c3ef9ff96c 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 You have new mail in /var/spool/mail/root
# 删除空白行
[root@C67-X64-A1 ~]# sed '/^$/d' /etc/fstab # # /etc/fstab # Created by anaconda on Tue Jul 5 23:40:37 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 # UUID=922eb46f-7e6e-4670-8bf1-6f9f1b05a053 / ext4 defaults 1 1 UUID=3881dbb7-5500-4eff-ba82-17651d436ce5 /boot ext4 defaults 1 2 UUID=419bb1cd-93b2-403b-9bf2-847d3072248b /tmp ext4 defaults 1 2 UUID=bcb87417-3008-4fcc-80d7-41c3ef9ff96c 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
# 在/tmp/fst的第二行后面插入"wanwan=3"
[root@C67-X64-A1 ~]# sed -i '2a wanwan=3' /tmp/fst You have new mail in /var/spool/mail/root [root@C67-X64-A1 ~]# cat /tmp/fst # wanwan=3 # /etc/fstab # Created by anaconda on Tue Jul 5 23:40:37 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 # UUID=922eb46f-7e6e-4670-8bf1-6f9f1b05a053 / ext4 defaults 1 1 UUID=3881dbb7-5500-4eff-ba82-17651d436ce5 /boot ext4 defaults 1 2 UUID=419bb1cd-93b2-403b-9bf2-847d3072248b /tmp ext4 defaults 1 2 UUID=bcb87417-3008-4fcc-80d7-41c3ef9ff96c 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
# 将“wanwan=3”替换成“wanwan=5”
[root@C67-X64-A1 ~]# sed -i 's/wanwan=3/wanwan=5/g' /tmp/fst [root@C67-X64-A1 ~]# cat /tmp/fst # I am the third I am the forth/p wanwan=5 # /etc/fstab # Created by anaconda on Tue Jul 5 23:40:37 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 # UUID=922eb46f-7e6e-4670-8bf1-6f9f1b05a053 / ext4 defaults 1 1 UUID=3881dbb7-5500-4eff-ba82-17651d436ce5 /boot ext4 defaults 1 2 UUID=419bb1cd-93b2-403b-9bf2-847d3072248b /tmp ext4 defaults 1 2 UUID=bcb87417-3008-4fcc-80d7-41c3ef9ff96c 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-5行的内容,i(inplace)直接就可以生效了,建议先测试后,然后再用“-i”参数
[root@C67-X64-A1 ~]# sed -i '1,5d' /tmp/fst [root@C67-X64-A1 ~]# cat /tmp/fst # /etc/fstab # Created by anaconda on Tue Jul 5 23:40:37 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 # UUID=922eb46f-7e6e-4670-8bf1-6f9f1b05a053 / ext4 defaults 1 1 UUID=3881dbb7-5500-4eff-ba82-17651d436ce5 /boot ext4 defaults 1 2 UUID=419bb1cd-93b2-403b-9bf2-847d3072248b /tmp ext4 defaults 1 2 UUID=bcb87417-3008-4fcc-80d7-41c3ef9ff96c 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
# 在UUID的行后,直接加上“TYPE=test”
[root@C67-X64-A1 ~]# cat /tmp/fst # /etc/fstab # Created by anaconda on Tue Jul 5 23:40:37 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 # UUID=922eb46f-7e6e-4670-8bf1-6f9f1b05a053 / ext4 defaults 1 1 UUID=3881dbb7-5500-4eff-ba82-17651d436ce5 /boot ext4 defaults 1 2 UUID=419bb1cd-93b2-403b-9bf2-847d3072248b /tmp ext4 defaults 1 2 UUID=bcb87417-3008-4fcc-80d7-41c3ef9ff96c 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@C67-X64-A1 ~]# sed '/^UUID/a TYPE=test' /tmp/fst # /etc/fstab # Created by anaconda on Tue Jul 5 23:40:37 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 # UUID=922eb46f-7e6e-4670-8bf1-6f9f1b05a053 / ext4 defaults 1 1 TYPE=test UUID=3881dbb7-5500-4eff-ba82-17651d436ce5 /boot ext4 defaults 1 2 TYPE=test UUID=419bb1cd-93b2-403b-9bf2-847d3072248b /tmp ext4 defaults 1 2 TYPE=test UUID=bcb87417-3008-4fcc-80d7-41c3ef9ff96c swap swap defaults 0 0 TYPE=test 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@C67-X64-A1 ~]# sed '/^#/a \#我们需要努力!' /tmp/fst # /etc/fstab #我们需要努力! # Created by anaconda on Tue Jul 5 23:40:37 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 #我们需要努力! # #我们需要努力! UUID=922eb46f-7e6e-4670-8bf1-6f9f1b05a053 / ext4 defaults 1 1 UUID=3881dbb7-5500-4eff-ba82-17651d436ce5 /boot ext4 defaults 1 2 UUID=419bb1cd-93b2-403b-9bf2-847d3072248b /tmp ext4 defaults 1 2 UUID=bcb87417-3008-4fcc-80d7-41c3ef9ff96c 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@C67-X64-A1 ~]# sed '/^#/i \#我们不要放弃!' /tmp/fst #我们不要放弃! # /etc/fstab #我们不要放弃! # Created by anaconda on Tue Jul 5 23:40:37 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 #我们不要放弃! # UUID=922eb46f-7e6e-4670-8bf1-6f9f1b05a053 / ext4 defaults 1 1 UUID=3881dbb7-5500-4eff-ba82-17651d436ce5 /boot ext4 defaults 1 2 UUID=419bb1cd-93b2-403b-9bf2-847d3072248b /tmp ext4 defaults 1 2 UUID=bcb87417-3008-4fcc-80d7-41c3ef9ff96c 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
#将/etc/fstab中将以UUID开头的行存入到/root/fstab中
[root@C67-X64-A1 ~]# sed -n '/^UUID/w /root/fstab' /etc/fstab [root@C67-X64-A1 ~]# cat /root/fstab UUID=922eb46f-7e6e-4670-8bf1-6f9f1b05a053 / ext4 defaults 1 1 UUID=3881dbb7-5500-4eff-ba82-17651d436ce5 /boot ext4 defaults 1 2 UUID=419bb1cd-93b2-403b-9bf2-847d3072248b /tmp ext4 defaults 1 2 UUID=bcb87417-3008-4fcc-80d7-41c3ef9ff96c swap swap defaults 0 0
# 将/root/write.txt文件读入到/etc/fstab的第六行
[root@C67-X64-A1 ~]# awk -F ":" '{print $1}' /etc/passwd|head -5>/root/write.txt [root@C67-X64-A1 ~]# cat /root/write.txt root bin daemon adm lp [root@C67-X64-A1 ~]# sed '6r /root/write.txt' /etc/fstab # # /etc/fstab # Created by anaconda on Tue Jul 5 23:40:37 2016 # # Accessible filesystems, by reference, are maintained under '/dev/disk' root bin daemon adm lp # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=922eb46f-7e6e-4670-8bf1-6f9f1b05a053 / ext4 defaults 1 1 UUID=3881dbb7-5500-4eff-ba82-17651d436ce5 /boot ext4 defaults 1 2 UUID=419bb1cd-93b2-403b-9bf2-847d3072248b /tmp ext4 defaults 1 2 UUID=bcb87417-3008-4fcc-80d7-41c3ef9ff96c 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
#对write.txt文件执行sed.sh脚本指令
[root@C67-X64-A1 ~]# cat sed.sh # this is a test sed command /^$/d [root@C67-X64-A1 ~]# sed -f sed.sh write.txt root oo bin daemon adm lp
练习1:删除/boot/grub/grub.conf文件中所有以空白开头的行行首的空白字符
[root@C67-X64-A1 ~]# cat /boot/grub/grub.conf # grub.conf generated by anaconda # # Note that you do not have to rerun grub after making changes to this file # NOTICE: You have a /boot partition. This means that # all kernel and initrd paths are relative to /boot/, eg. # root (hd0,0) # kernel /vmlinuz-version ro root=/dev/sda5 # initrd /initrd-[generic-]version.img #boot=/dev/sda default=0 timeout=5 splashimage=(hd0,0)/grub/splash.xpm.gz hiddenmenu title CentOS 6 (2.6.32-573.el6.x86_64) root (hd0,0) kernel /tboot.gz logging=vga,serial,memory module /vmlinuz-2.6.32-573.el6.x86_64 ro root=UUID=922eb46f-7e6e-4670-8bf1-6f9f1b05a053 intel_iommu=on amd_iommu=on rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=128M.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet module /initramfs-2.6.32-573.el6.x86_64.img [root@C67-X64-A1 ~]# sed 's#^[[:space:]]\+##' /boot/grub/grub.conf # grub.conf generated by anaconda # # Note that you do not have to rerun grub after making changes to this file # NOTICE: You have a /boot partition. This means that # all kernel and initrd paths are relative to /boot/, eg. # root (hd0,0) # kernel /vmlinuz-version ro root=/dev/sda5 # initrd /initrd-[generic-]version.img #boot=/dev/sda default=0 timeout=5 splashimage=(hd0,0)/grub/splash.xpm.gz hiddenmenu title CentOS 6 (2.6.32-573.el6.x86_64) root (hd0,0) kernel /tboot.gz logging=vga,serial,memory module /vmlinuz-2.6.32-573.el6.x86_64 ro root=UUID=922eb46f-7e6e-4670-8bf1-6f9f1b05a053 intel_iommu=on amd_iommu=on rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=128M.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet module /initramfs-2.6.32-573.el6.x86_64.img
练习2:删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首和空白字符
[root@C67-X64-A1 ~]# sed 's@^#[[:space:]]\+@@' /etc/fstab # /etc/fstab Created by anaconda on Tue Jul 5 23:40:37 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 # UUID=922eb46f-7e6e-4670-8bf1-6f9f1b05a053 / ext4 defaults 1 1 UUID=3881dbb7-5500-4eff-ba82-17651d436ce5 /boot ext4 defaults 1 2 UUID=419bb1cd-93b2-403b-9bf2-847d3072248b /tmp ext4 defaults 1 2 UUID=bcb87417-3008-4fcc-80d7-41c3ef9ff96c 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
练习3:echo一个绝对路径路径sed命令,取出其基名:取出其目录名
目录名称: [root@C67-X64-A1 ~]# echo "/etc/sysconfig"|sed 's@[^/]\+/\?$@@' /etc/ [root@C67-X64-A1 ~]# echo "/etc/sysconfig/"|sed 's@[^/]\+/\?$@@' /etc/ 基名: [root@C67-X64-A1 ~]# echo "/etc/sysconfig/" | sed -r 's@^/.*/([^/]+)/?@\1@g' sysconfig [root@C67-X64-A1 ~]# echo "/etc/sysconfig" | sed -r 's@^/.*/([^/]+)/?@\1@g' sysconfig
# 执行多个指令的方法
[root@C67-X64-A1 tmp]# cat eth0 DEVICE=eth0 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=dhcp 方法1: [root@C67-X64-A1 tmp]# sed -e 's/yes/no/' -e 's/eth0/ens32/' eth0 DEVICE=ens32 TYPE=Ethernet ONBOOT=no NM_CONTROLLED=no BOOTPROTO=dhcp 方法2:利用分号隔开 [root@C67-X64-A1 tmp]# sed 's/yes/no/;s/eth0/ens32/' eth0 DEVICE=ens32 TYPE=Ethernet ONBOOT=no NM_CONTROLLED=no BOOTPROTO=dhcp 方法3:利用分行 [root@C67-X64-A1 tmp]# sed ' > s/yes/no/ > s/eth0/ens32/' eth0 DEVICE=ens32 TYPE=Ethernet ONBOOT=no NM_CONTROLLED=no BOOTPROTO=dhcp
# 操作地址匹配
sed -n '1~2p' /etc/passwd
# 删除/tmp/passwd的前5行
[root@C67-X64-A1 tmp]# head -10 /etc/passwd root:x:0:0:root:/root:/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 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin 删除后,然后进行对比: [root@C67-X64-A1 tmp]# sed -i '1,5d' /tmp/passwd [root@C67-X64-A1 tmp]# cat /tmp/passwd |head -10 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 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin gopher:x:13:30:gopher:/var/gopher:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin
# 替换第二个body位/body
[root@C67-X64-A1 ~]# cat test.txt <html> <title>First Web</title> <body>hello world!<body> </html> [root@C67-X64-A1 ~]# cat sed.sh /body/{ s//\/body/2 } [root@C67-X64-A1 ~]# sed -f sed.sh test.txt <html> <title>First Web</title> <body>hello world!</body> </html>
# 利用 sed 直接在 /tmp/passwd 最后一行加入『# This is a test』
# sed -i '$a # This is a test' /tmp/passwd # 先不用-i命令,进行测试下 [root@zabbix ~]# sed '$a # this is a test' /tmp/passwd |tail -5 awx:x:988:985::/var/lib/awx:/bin/bash li4:x:1011:1011::/home/li4:/bin/bash admin:x:1012:1012::/home/admin:/sbin/nologin monitor:x:1013:1013::/home/monitor:/bin/bash # this is a test 说明: a)由于$代表的是最后一行,而 a 的动作是新增,因此该文件最后新增『# This is a test』! b)sed 的『 -i 』选项可以直接修改文件内容,这功能非常有帮助! c)举例来说,如果你有一个 100 万行的文件,你要在第 100 行加某些文字,此时使用 vim 可能会疯掉!因为文件太大了!那怎办? 就利用 sed 啊!透过 sed 直接修改/取代的功能,你甚至不需要使用 vim 去修订!
#在/tmp/passwd第二行后面添加"# this is a test"
[root@zabbix ~]# sed '2a # this is a test' /tmp/passwd |head -5 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin # this is a test daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin
# 利用 sed 将passwd内每一行结尾若为 . 则换成 !
[root@zabbix tmp]# cat passwd James is a good man . Kobe is good at IT . I love Linux. welcome to linux. [root@zabbix tmp]# sed 's/\.$/\!/g' passwd James is a good man ! Kobe is good at IT ! I love Linux! welcome to linux! 使用-i参数才会真正的生效
原创文章,作者:Net21-冰冻vs西瓜,如若转载,请注明出处:http://www.178linux.com/25966