- 积分
- 88
- 鸿鹄币
- 个
- 好评度
- 点
- 精华
- 注册时间
- 2016-6-21
- 最后登录
- 1970-1-1
- 阅读权限
- 20
- 听众
- 收听
助理工程师
|
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
=: 只显示匹配行的行号
北京网站建设公司
|
|