一,sed:行编辑器
sed [options] ‘AddressCommand’ file …
-n: 静默模式,不再默认显示模式空间中的内容
-i: 直接修改原文件
-e SCRIPT -e SCRIPT:可以同时执行多个脚本
-f /PATH/TO/SED_SCRIPT
-r: 表示使用扩展正则表达式
sed:行编辑器Command
d: 删除符合条件的行;
p: 显示符合条件的行;
a \string: 在指定的行后面追加新行,内容为string
\n:可以用于换行
i \string: 在指定的行前面添加新行,内容为string
r FILE: 将指定的文件的内容添加至符合条件的行处
w FILE: 将地址指定的范围内的行另存至指定的文件中;
s/pattern/string/修饰符: 查找并替换,默认只替换每行中第一次被模式匹配到的字符串
g: 行内全局替换
i: 忽略字符大小写
s///: s###, s@@@
(), \1, \2
sed:行编辑器Address
可以没有
给定范围
查找指定行/str/
[root@node01 ~]# cat sort.txt
nana 12
apple 1
orange 8
[root@node01 ~]# sed "2p" sort.txt
nana 12
apple 1
apple 1
orange 8
[root@node01 ~]# sed -n "2p" sort.txt
apple 1
[root@node01 ~]# sed "3d" sort.txt
nana 12
apple 1
[root@node01 ~]# cat sort.txt
nana 12
apple 1
orange 8
# -i: 直接修改原文件
[root@node01 ~]# sed -i "3d" sort.txt
[root@node01 ~]# cat sort.txt
nana 12
apple 1
[root@node01 ~]# sed "2i\sxt" sort.txt
nana 12
sxt
apple 1
[root@node01 ~]# sed -i "2a\sxt" sort.txt
[root@node01 ~]# cat sort.txt
nana 12
sxt
sxt
apple 1
[root@node01 ~]# sed "s/sxt/hello/" sort.txt
nana 12
hello
hello
apple 1
[root@node01 ~]# sed "/sxt/d" sort.txt
nana 12
apple 1
二,举例说明
[root@node01 ~]# cp /etc/inittab .
[root@node01 ~]# ls
123.bak abc anaconda-ks.cfg grep.txt inittab install.log install.log.syslog passwd profile sort.txt wangle x y
[root@node01 ~]# cat inittab
# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# System initialization is started by /etc/init/rcS.conf
#
# Individual runlevels are started by /etc/init/rc.conf
#
# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
#
# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
# with configuration in /etc/sysconfig/init.
#
# For information on how to write upstart event handlers, or how
# upstart works, see init(5), init(8), and initctl(8).
#
# Default runlevel. The runlevels used are:
# 0 - halt (Do NOT set initdefault to this)
# 1 - Single user mode
# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)
# 3 - Full multiuser mode
# 4 - unused
# 5 - X11
# 6 - reboot (Do NOT set initdefault to this)
#
id:3:initdefault:
[root@node01 ~]# sed "s/[0-9]/8/" inittab
# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# System initialization is started by /etc/init/rcS.conf
#
# Individual runlevels are started by /etc/init/rc.conf
#
# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
#
# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
# with configuration in /etc/sysconfig/init.
#
# For information on how to write upstart event handlers, or how
# upstart works, see init(8), init(8), and initctl(8).
#
# Default runlevel. The runlevels used are:
# 8 - halt (Do NOT set initdefault to this)
# 8 - Single user mode
# 8 - Multiuser, without NFS (The same as 3, if you do not have networking)
# 8 - Full multiuser mode
# 8 - unused
# 8 - X11
# 8 - reboot (Do NOT set initdefault to this)
#
id:8:initdefault:
[root@node01 ~]# sed "s/id:3:initdefault:/8/" inittab
# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# System initialization is started by /etc/init/rcS.conf
#
# Individual runlevels are started by /etc/init/rc.conf
#
# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
#
# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
# with configuration in /etc/sysconfig/init.
#
# For information on how to write upstart event handlers, or how
# upstart works, see init(5), init(8), and initctl(8).
#
# Default runlevel. The runlevels used are:
# 0 - halt (Do NOT set initdefault to this)
# 1 - Single user mode
# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)
# 3 - Full multiuser mode
# 4 - unused
# 5 - X11
# 6 - reboot (Do NOT set initdefault to this)
#
8
[root@node01 ~]# sed "s/id:3:initdefault:/id8:initdefault:/" inittab
# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# System initialization is started by /etc/init/rcS.conf
#
# Individual runlevels are started by /etc/init/rc.conf
#
# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
#
# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
# with configuration in /etc/sysconfig/init.
#
# For information on how to write upstart event handlers, or how
# upstart works, see init(5), init(8), and initctl(8).
#
# Default runlevel. The runlevels used are:
# 0 - halt (Do NOT set initdefault to this)
# 1 - Single user mode
# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)
# 3 - Full multiuser mode
# 4 - unused
# 5 - X11
# 6 - reboot (Do NOT set initdefault to this)
#
id8:initdefault: