sed行编辑工具的基本用法
sed:stream editor
流编辑器,行编辑器
工作原理:
sed是行编辑器,所以是以行读取,将文本的每一行读取到sed的内存空间,我们称之为模式空间,在模式空间中做模式匹配,若不能匹配,则默认输出到标准输出中,能匹配到的内容则执行编辑操作,默认会把编辑后的内容输出到标准输出中(删除则不会)
命令使用:
sed [option...] ‘script‘ file
options:
-n:静默模式,不输出模式匹配空间的内容
-r:支持正则表达式
-f /path/to/sed_script:读取sed脚本执行编辑
-e scripts -e scripts:多点编辑,执行多个编辑命令(注意先后顺序)
使用管道或脚本文件也可执行多个编辑操作
script:
是sed命令的地址定界及编辑命令
地址定界:
空地址:对全文处理
单地址:
#:指定行,#代表数字
/pattern/:被模式匹配到的每一行
范围地址:
#,#:第#行到第#行
#,+#:从#行开始往后#行
#,/pattern1/:从#行到模式所匹配到的行
/pattren1/,/pattern2/:从模式匹配到的行到另一模式匹配到的行
步进地址:~步进符号
1~2:所有奇数行
2~2:所有偶数行
使用模式匹配的时候要使用斜线围起来
编辑命令:
d:删除
p:显示模式空间匹配的行(默认就输出,一般搭配-n使用)
a \txt:在匹配到行的下面插入指定字符
i \txt:在匹配到行的上面插入指定字符
r /path/to/somefile:在匹配到位置后一行插入指定文件中的内容
w /path/to/somefile:将匹配的到内容保存(覆盖)到指定文件中
=:显示符合条件行的行号
s/查找条件/替换内容/:替换,查找可使用模式匹配,替换内容不行
sed ‘地址定界s@查找条件@替换文本@’
修饰符:
g:glob全局替换
i:不区分大小写
使用s///替换默认是全文替换
效果演示:
删除:d
[root@johnson‘s linux ~]# sed ‘/^#/d‘ /etc/fstab UUID=385752f1-421d-4e36-a2cf-a160191d0ffa / ext4 defaults 1 1 UUID=03d99d39-1fbf-454e-bb6a-2ddf09cb1668 /boot ext4 defaults 1 2 UUID=165d5587-8872-402c-b8e1-fc139857d23f 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(一般搭配-n使用)
[root@johnson‘s linux ~]# sed -n ‘/^#/p‘ /etc/fstab # # /etc/fstab # Created by anaconda on Mon Feb 22 04:51:14 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 # [root@johnson‘s linux ~]#
没有使用-n选项使用p的效果就是会重复显示出被模式所匹配到的行
[root@johnson‘s linux ~]# sed ‘/^#/p‘ /etc/fstab # # # /etc/fstab # /etc/fstab # Created by anaconda on Mon Feb 22 04:51:14 2016 # Created by anaconda on Mon Feb 22 04:51:14 2016 # # # 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 # # UUID=385752f1-421d-4e36-a2cf-a160191d0ffa / ext4 defaults 1 1 UUID=03d99d39-1fbf-454e-bb6a-2ddf09cb1668 /boot ext4 defaults 1 2 UUID=165d5587-8872-402c-b8e1-fc139857d23f 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
在匹配到的行上面插入字符:i \txt
[root@johnson‘s linux ~]# sed ‘/^UUID/i \this is a new line‘ /etc/fstab # # /etc/fstab # Created by anaconda on Mon Feb 22 04:51:14 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 # this is a new line UUID=385752f1-421d-4e36-a2cf-a160191d0ffa / ext4 defaults 1 1 this is a new line UUID=03d99d39-1fbf-454e-bb6a-2ddf09cb1668 /boot ext4 defaults 1 2 this is a new line UUID=165d5587-8872-402c-b8e1-fc139857d23f swap s
在匹配到的行下面插入字符:a \txt
[root@johnson‘s linux ~]# sed ‘/^UUID/a \this is a new line‘ /etc/fstab # # /etc/fstab # Created by anaconda on Mon Feb 22 04:51:14 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=385752f1-421d-4e36-a2cf-a160191d0ffa / ext4 defaults 1 1 this is a new line UUID=03d99d39-1fbf-454e-bb6a-2ddf09cb1668 /boot ext4 defaults 1 2 this is a new line UUID=165d5587-8872-402c-b8e1-fc139857d23f swap swap defaults 0 0 this is a new line
插入指定的文件的内容:r /path/to/file
[root@johnson‘s linux ~]# cat sed.txt he like his liker he like his lover she love her liker she love her lover
[root@johnson‘s linux ~]# sed ‘/^#[[:space:]]\//r ./sed.txt‘ /etc/fstab # # /etc/fstab he like his liker he like his lover she love her liker she love her lover # Created by anaconda on Mon Feb 22 04:51:14 2016 #
保存被匹配到的行到指定的文件中:w /path/to/somewhere
[root@johnson‘s linux ~]# sed -n ‘/^UUID/w ./UUID_file‘ /etc/fstab [root@johnson‘s linux ~]# cat UUID_file UUID=385752f1-421d-4e36-a2cf-a160191d0ffa / ext4 defaults 1 1 UUID=03d99d39-1fbf-454e-bb6a-2ddf09cb1668 /boot ext4 defaults 1 2 UUID=165d5587-8872-402c-b8e1-fc139857d23f swap swap defaults 0 0 [root@johnson‘s linux ~]#
显示符合条件行的行号:=
[root@johnson‘s linux ~]# sed -n ‘/^UUID/=‘ /etc/fstab 9 10 11 [root@johnson‘s linux ~]# 这个显示行号加-n选项意味着只输出行号而不输出其行的内容
实例:
1)替换/etc/inittab文件中”id:3:initdefault:"一行中的数字为5
[root@johnson‘s linux ~]# sed ‘s@\(id:\)[0-9]@\15@‘ /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:5:initdefault:
2)删除/etc/init.d/funcions文件中的空白行
[root@johnson‘s linux ~]# sed ‘/^$/d‘ /etc/init.d/functions # -*-Shell-script-*- # # functions This file contains functions to be used by most or all # shell scripts in the /etc/init.d directory. # TEXTDOMAIN=initscripts # Make sure umask is sane
3)删除/etc/inittab文件中位于行首的#;
[root@johnson‘s linux ~]# cat /etc/inittab(先用cat命令查看原来的文本) # 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 [root@johnson‘s linux ~]# sed ‘s@^#@@‘ /etc/inittab(使用sed替换的方式来删掉#) 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
4)删除/boot/grub/grub.conf文件中行首的空白字符。
[root@johnson‘s linux ~]# cat /boot/grub/grub.conf(cat先看原文本效果) # 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/sda2 # 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 /vmlinuz-2.6.32-573.el6.x86_64 ro root=UUID=385752f1-421d-4e36-a2cf-a160191d0ffa rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet initrd /initramfs-2.6.32-573.el6.x86_64.img
[root@johnson‘s linux ~]# 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/sda2 # 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 /vmlinuz-2.6.32-573.el6.x86_64 ro root=UUID=385752f1-421d-4e36-a2cf-a160191d0ffa rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet initrd /initramfs-2.6.32-573.el6.x86_64.img (最后三行的空格被删除了)
5)取出一个文件路径的目录名称,如/etc/sysconfig/network,其目录为/etc/sysconfig,功能类似dirname命令
[root@johnson‘s linux ~]# echo /etc/sysconfig/network | sed ‘s@[^/]\+/\?$@@‘ /etc/sysconfig/ [root@johnson‘s linux ~]#
最后一题略有难度,思路就是将行尾的字符串去掉,所以是以非/开头,并且字符串在行尾,所以要锚定行尾,"/\?"表示字符串后面的/可有可无
本文出自 “Rock blog” 博客,请务必保留此出处http://johnsonxu.blog.51cto.com/11214707/1753232
原文:http://johnsonxu.blog.51cto.com/11214707/1753232