首页 > 代码库 > sed 命令操作
sed 命令操作
sed命令
-n 静默模式,不输出模式空间中的内容
编辑命令:
d:删除
地址定界:
startline,endline
1,3
/part1/,/part2/ 匹配范围之间
/part/ 匹配到所匹配的字符
-n:静默模式,只显示匹配字符串或者行.
a 在当前行下面插入文本。
i 在当前行上面插入文本。
r:在匹配航之后引入另一个文件内容.
w:将匹配到的行输出到另一个文件.
p:将匹配的行打印出来(也会输出原文件的内容),pn只显示打印的行.
[[:upper:]] 匹配以大写字母开头的行.
=:显示匹配行的行号,-n和=同时使用,只输出匹配行的行号.
[^]:匹配一个不在指定范围内的字符.
^ 匹配行开始,如:/^sed/匹配所有以sed开头的行。
$ 匹配行结束,如:/sed$/匹配所有以sed结尾的行。
. 匹配一个非换行符的任意字符,如:/s.d/匹配s后接一个任意字符,最后是d。 * 匹配0个或多个字符,如:/*sed/匹配所有模板是一个或多个空格后紧跟sed的行。
-f /path/to/sed_script_file :指定sed脚本文件.
-e ‘script‘ -e ‘script‘:指定多个编辑命令
正则表达式:
基本的:
字符:. [] [^]
次数:* \? \{\}
锚定:^ $ \< \>
分组:\(\)
引用:\1,\2,...
扩展:
字符:. [] [^]
[[:lower:]]:匹配所有的小写
[[:punct:]]:匹配所有的标点符号
[[:alnum:]]:匹配所有的数字
次数:
* ? + {m,n}
锚定:
^ $ \> \<
或:
a|b
分组:
()
引用:
\1,\2
sed ‘地址定界s@查找条件@替换文本@‘ :在指定的定界内查找匹配到的条件做替换
修饰符:
g:global,全局替换
i:ignore-case不区分大小写
1)、[root@localhost ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Feb 24 02:20:51 2017
#
# 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
#
/dev/mapper/VolGroup-lv_root / ext4 defaults 1 1
UUID=2020ea71-49cf-488e-bd6c-de72a001082b /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
[root@localhost ~]# sed ‘1,3d‘ /etc/fstab
# Created by anaconda on Fri Feb 24 02:20:51 2017 # # 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 # /dev/mapper/VolGroup-lv_root / ext4 defaults 1 1 UUID=2020ea71-49cf-488e-bd6c-de72a001082b /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
-n 参数
sed -n ‘1,3d‘ /etc/fstab 不会输出任何东西,静默模式
2)、删除所有以#号开头的行:
[root@localhost ~]# sed ‘/^#/d‘ /etc/fstab /dev/mapper/VolGroup-lv_root / ext4 defaults 1 1 UUID=2020ea71-49cf-488e-bd6c-de72a001082b /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
3)、删除所有以/ 开头的行:
[root@localhost ~]# sed ‘/^\//d‘ /etc/fstab
# # /etc/fstab # Created by anaconda on Fri Feb 24 02:20:51 2017 # # 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=2020ea71-49cf-488e-bd6c-de72a001082b /boot ext4 defaults 1 2 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
4)、删除从第2行开始,到第一次以“/”开头的行:
[root@localhost ~]# sed ‘2,/^\//d‘ /etc/fstab
UUID=2020ea71-49cf-488e-bd6c-de72a001082b /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
5)、删除第一个#号开头的行到第一个"/",开头的行:
[root@localhost ~]# sed ‘/^#/,/^\//d‘ /etc/fstab
UUID=2020ea71-49cf-488e-bd6c-de72a001082b /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
-p 打印参数
6)、打印第一个#号开头的行到第一个"/",开头的行,如果不加-n参数静默模式,会输出重复匹配的内容:
[root@localhost ~]# sed -n ‘/^#/,/^\//p‘ /etc/fstab
# # /etc/fstab # Created by anaconda on Fri Feb 24 02:20:51 2017 # # 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 # /dev/mapper/VolGroup-lv_root / ext4 defaults 1 1
不加-n参数结果:
[root@localhost ~]# sed ‘/^#/,/^\//p‘ /etc/fstab
# # # /etc/fstab # /etc/fstab # Created by anaconda on Fri Feb 24 02:20:51 2017 # Created by anaconda on Fri Feb 24 02:20:51 2017 # # # Accessible filesystems, by reference, are maintained under ‘/dev/disk‘ # 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 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # # /dev/mapper/VolGroup-lv_root / ext4 defaults 1 1 /dev/mapper/VolGroup-lv_root / ext4 defaults 1 1 UUID=2020ea71-49cf-488e-bd6c-de72a001082b /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
a和i参数:
a:在匹配的行之后添加内容
i: 在匹配的行之前添加内容
#再#号开头的行之后添加hello world
# sed ‘/^#/a \hello world‘ /etc/fstab
# hello world # /etc/fstab hello world # Created by anaconda on Fri Feb 24 02:20:51 2017 hello world # hello world # Accessible filesystems, by reference, are maintained under ‘/dev/disk‘ hello world # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info hello world # hello world /dev/mapper/VolGroup-lv_root / ext4 defaults 1 1 UUID=2020ea71-49cf-488e-bd6c-de72a001082b /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
#在匹配的行之前添加hello world字段。
# sed ‘/^\//i \hello world‘ /etc/fstab
# # /etc/fstab # Created by anaconda on Fri Feb 24 02:20:51 2017 # # 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 # hello world /dev/mapper/VolGroup-lv_root / ext4 defaults 1 1 UUID=2020ea71-49cf-488e-bd6c-de72a001082b /boot ext4 defaults 1 2 hello world /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
匹配在大写字母开头的行之后添加二行,第一行hello,第二行second。注释:匹配大写字母以[[:upper:]]
# sed ‘/^[[:upper:]]/a \hello \nsecond‘ /etc/fstab
# # /etc/fstab # Created by anaconda on Fri Feb 24 02:20:51 2017 # # 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 # /dev/mapper/VolGroup-lv_root / ext4 defaults 1 1 UUID=2020ea71-49cf-488e-bd6c-de72a001082b /boot ext4 defaults 1 2 hello second /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
r 命令 /path/to/somefile :在指定位置把另一个文件的内容插入进来。
# sed ‘/^UUID/r /etc/issue‘ /etc/fstab
# # /etc/fstab # Created by anaconda on Fri Feb 24 02:20:51 2017 # # 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 # /dev/mapper/VolGroup-lv_root / ext4 defaults 1 1 UUID=2020ea71-49cf-488e-bd6c-de72a001082b /boot ext4 defaults 1 2 CentOS release 6.5 (Final) Kernel \r on an \m /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
w:/path/to/somefile:将符合条件的所有行保存至指定文件
# sed ‘/^UUID/w /tmp/uuid.txt‘ /etc/fstab # # /etc/fstab # Created by anaconda on Fri Feb 24 02:20:51 2017 # # 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 # /dev/mapper/VolGroup-lv_root / ext4 defaults 1 1 UUID=2020ea71-49cf-488e-bd6c-de72a001082b /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 查看/tmp/uuid.txt的文件内容。 [root@localhost ~]# cat /tmp/uuid.txt UUID=2020ea71-49cf-488e-bd6c-de72a001082b /boot ext4 defaults 1 2
=: 只显示匹配行的行号
# sed ‘/^UUID/=‘ /etc/fstab
# # /etc/fstab # Created by anaconda on Fri Feb 24 02:20:51 2017 # # 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 # /dev/mapper/VolGroup-lv_root / ext4 defaults 1 1 10 UUID=2020ea71-49cf-488e-bd6c-de72a001082b /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
# sed -n ‘/^UUID/=‘ /etc/fstab
10
s///:查找条件可以使用模式,但要替换的内容不能使用
# cat sed.txt
He like his liker.
He like his lover.
She love her liker.
She love her lover.
sed 匹配使用‘//‘,匹配内容(l..e)的行,.* 表示后面出现多次的,1表示第一次匹配到的前两行匹配到like,后两行匹配到love。1r代表liker和lover.
# sed ‘/\(l..e\).*\1r/d‘ sed.txt
He like his lover. She love her liker.
#去掉r匹配也能成功.
# sed ‘/\(l..e\).*\1/d‘ sed.txt
He like his lover. She love her liker.
行首为#号开头的行替换为空行。
# sed ‘s/^#//g‘ /etc/fstab
/etc/fstab Created by anaconda on Wed Apr 19 00:39:18 2017 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 /dev/mapper/VolGroup-lv_root / ext4 defaults 1 1 UUID=1f522592-b116-4599-b4b8-2e292acc984a /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
删除空白行:
root@localhost ~]# sed ‘/^$/d‘ /etc/fstab
# # /etc/fstab # Created by anaconda on Wed Apr 19 00:39:18 2017 # # 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 # /dev/mapper/VolGroup-lv_root / ext4 defaults 1 1 UUID=1f522592-b116-4599-b4b8-2e292acc984a /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
替换行首以#行开头的行后面跟至少一个空白字符的行替为空.
sed ‘s/^#[[:space:]]\{1,\}//g‘ /etc/fstab
sed ‘s/^#[[:space:]]\+//g‘ /etc/fstab
# /etc/fstab Created by anaconda on Wed Apr 19 00:39:18 2017 # 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 # /dev/mapper/VolGroup-lv_root / ext4 defaults 1 1 UUID=1f522592-b116-4599-b4b8-2e292acc984a /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
删除以空白为行首开始的行的空白
sed ‘s/^[[:space:]]\+//‘ /etc/grub.conf
sed ‘s/^[[:space:]]\{1,\}//‘ /etc/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/mapper/VolGroup-lv_root # initrd /initrd-[generic-]version.img #boot=/dev/sda default=0 timeout=5 splashimage=(hd0,0)/grub/splash.xpm.gz hiddenmenu title CentOS (2.6.32-431.el6.x86_64) root (hd0,0) kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup/lv_root KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet initrd /initramfs-2.6.32-431.el6.x86_64.img
# echo /etc/sysconfig/network | sed ‘s@[^/]\{1,\}/\?$@@‘
/etc/sysconfig/
-f sed执行运行的脚本文件,如果多行可以换行写.
[root@localhost ~]# echo /etc/sysconfig/network | sed ‘s@[^/]\{1,\}/\?$@@‘ /etc/sysconfig/ [root@localhost ~]# vim 2.txt [root@localhost ~]# echo /etc/sysconfig/network | sed -f 2.txt /etc/sysconfig/ [root@localhost ~]# cat 2.txt s@[^/]\{1,\}/\?$@@
[root@localhost ~]# sed ‘s/^#//g‘ /etc/fstab | sed ‘/^$/d‘
/etc/fstab
Created by anaconda on Wed Apr 19 00:39:18 2017
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
/dev/mapper/VolGroup-lv_root / ext4 defaults 1 1
UUID=1f522592-b116-4599-b4b8-2e292acc984a /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
[root@localhost ~]# sed -f 2.txt /etc/fstab
/etc/fstab
Created by anaconda on Wed Apr 19 00:39:18 2017
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
/dev/mapper/VolGroup-lv_root / ext4 defaults 1 1
UUID=1f522592-b116-4599-b4b8-2e292acc984a /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
[root@localhost ~]# cat 2.txt
s/^#//g
/^$/d
-e :指定多个编辑命令.
[root@localhost ~]# sed -e ‘s/^#//g‘ -e ‘/^$/d‘ /etc/fstab
/etc/fstab
Created by anaconda on Wed Apr 19 00:39:18 2017
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
/dev/mapper/VolGroup-lv_root / ext4 defaults 1 1
UUID=1f522592-b116-4599-b4b8-2e292acc984a /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
本文出自 “蚂蚁” 博客,请务必保留此出处http://215687833.blog.51cto.com/6724358/1928468
sed 命令操作