sed命令简介:
文本编辑器,流编辑器,行编辑器.默认不编辑原文件, 仅对模式空间中的数据做处理;
而后处理结束后,将模式空间打印至屏幕;
1.命令格式:
sed [option]‘addresscommand‘ file ...对这个或这些文件的某一段进行编辑
Address:(表示操作文本范围)
1、StartLine,EndLine
比如1,100
$:最后一行
2、/RegExp/ 使用正则表达式来匹配模式 必须使用//
/^root/
3、/pattern1/,/pattern2/
第一次被pattern1匹配到的行开始,至第一次被pattern2匹配到的行结束,这中间的所有行
4、LineNumber
指定某一行
5、StartLine, +N
从startLine开始,向后的N行;
Command:(表示操作命令)
d: 删除符合条件的行;
p: 显示符合条件的行;
a \string: 在指定的行后面追加新行,内容为string
\n:可以用于换行
i \string: 在指定的行前面添加新行,内容为string
r FILE: 将指定的文件的内容添加至符合条件的行处
w FILE: 将地址指定的范围内的行另存至指定的文件中;
s/pattern/string/修饰符: 查找并替换,默认只替换每行中第一次被模式
匹配到的字符串(pattern 被查找的字符串,string替换为的字符串)
s/pattern/string/g 这样表示所有查到的字符串都被替换掉
加修饰符
g: 全局替换
i: 忽略字符大小写
s///: s###, [email protected]@@ 分割符也可以用###和@@@作为分隔符
\(\), \1, \2
l..e: like-->liker
love-->lover
like-->Like
love-->Love sed ‘s/l(..e)/L\1/g‘
&: 引用模式匹配整个串
sed ‘s/\(l..e\)/\1r/g‘ = sed ‘s/l..e/&r/g‘
2.命令功能:
文本编辑器,流编辑器,行编辑器.默认不编辑原文件, 仅对模式空间中的数据做处理;
而后处理结束后,将模式空间打印至屏幕;
3.命令参数:
-n: 静默模式,不再默认显示模式空间中的内容
-i: 直接修改原文件
-e SCRIPT -e SCRIPT:可以同时执行多个脚本
-f /PATH/TO/SED_SCRIPT
sed -f /path/to/scripts file
表示/path/to/scripts的脚本都对file文件执行
-r: 表示使用扩展正则表达式
4.命令实例:
1、删除/etc/grub.conf文件中行首的空白符;
sed -r ‘s/^[[:space:]]+//g‘ /etc/grub.conf
[[email protected] ~]# sed -r ‘s/^[[:space:]]+//g‘ /etc/grub.conf
# 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/sda2
# initrd /initrd-[generic-]version.img
#boot=/dev/sda
2、替换/etc/inittab文件中“id:3:initdefault:”一行中的数字为5;
sed ‘s/\(id:\)[[:digit:]]\(:initdefault:\)/\15\2/g‘ /etc/inittab
[[email protected] ~]# sed ‘s/\(id:\)[[:digit:]]\(:initdefault:\)/\15\2/g‘ /etc/inittab
# 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:5:initdefault:
3、删除/etc/inittab文件开头的#;
sed ‘s/^#//g‘ /etc/inittab
[[email protected] ~]# sed ‘s/^#//g‘ /etc/inittab
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:
4、删除/etc/inittab文件中空白行;
sed ‘/^$/d‘ /etc/inittab
5、删除某文件中开头的#号及后面的空白字符,但要求#号后面必须有空白字符;
sed ‘s/^#[[:space:]]\{1,\}//g‘ /etc/inittab
或者sed -r ‘s/^#[[:space:]]+//g‘ /etc/inittab
[[email protected] ~]# sed ‘s/^#[[:space:]]\{1,\}//g‘ /etc/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:
6、删除某文件中以空白字符后面跟#类的行中的开头的空白字符及#
sed ‘s/^[[:space:]]\{1,\}#//g‘
7、取出一个文件路径的目录名称;
echo "/etc/inittab/" | sed -r ‘s#^(/.*/)[^/]+/?#\1#g‘
[[email protected] test]# echo "/etc/inittab/" | sed -r ‘s#^(/.*/)[^/]+/?#\1#g‘
/etc/
取文件名(基名):
echo "/etc/inittab/" | sed -r ‘s#^/.*/([^/]+)/?#\1#g‘
[[email protected] test]# echo "/etc/inittab/" | sed -r ‘s#^/.*/([^/]+)/?#\1#g‘
inittab