1、删除/etc/grub2.conf文件中所有以空白开头的行行首的空白字符
[root@localhost ~]# sed -r ‘s/^[[:space:]]+//g‘ /etc/grub2.cfg # # DO NOT EDIT THIS FILE # # It is automatically generated by grub2-mkconfig using templates # from /etc/grub.d and settings from /etc/default/grub # ### BEGIN /etc/grub.d/00_header ### set pager=1 if [ -s $prefix/grubenv ]; then load_env ......
2、删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首的#和空白字符
[root@localhost ~]# sed -r ‘s@^#[[:space:]]+\b@@g‘ /etc/fstab # # /etc/fstab Created by anaconda on Wed Aug 3 06:08:10 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=1aa7af5a-15a0-4cf2-a11d-8304d67c7093 / xfs defaults 0 0 UUID=2fb41265-5d13-4ef0-82f6-3147009bd1a5 /boot xfs defaults 0 0 UUID=a5a6b450-ba91-4e5b-a5ad-a17e12a2a994 /testdir xfs defaults 0 0 UUID=1d6f94c4-7eb4-430c-b143-b63b498c1c98 swap swap defaults 0 0
3、在/root/install.log每一行行首增加#号
[root@localhost ~]# sed ‘s/^/#/g‘ /etc/fstab
# ## ## /etc/fstab ## Created by anaconda on Wed Aug 3 06:08:10 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=1aa7af5a-15a0-4cf2-a11d-8304d67c7093 / xfs defaults 0 0 #UUID=2fb41265-5d13-4ef0-82f6-3147009bd1a5 /boot xfs defaults 0 0 #UUID=a5a6b450-ba91-4e5b-a5ad-a17e12a2a994 /testdir xfs defaults 0 0 #UUID=1d6f94c4-7eb4-430c-b143-b63b498c1c98 swap swap defaults 0 0
4、在/etc/fstab文件中不以#开头的行的行首增加#号
[root@localhost ~]# sed ‘s/^[^#]/#&/g‘ /etc/fstab # # /etc/fstab # Created by anaconda on Wed Aug 3 06:08:10 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=1aa7af5a-15a0-4cf2-a11d-8304d67c7093 / xfs defaults 0 0 #UUID=2fb41265-5d13-4ef0-82f6-3147009bd1a5 /boot xfs defaults 0 0 #UUID=a5a6b450-ba91-4e5b-a5ad-a17e12a2a994 /testdir xfs defaults 0 0 #UUID=1d6f94c4-7eb4-430c-b143-b63b498c1c98 swap swap defaults 0 0
注意:最上面的回车换行没有加#,即没有匹配回车换行
[root@localhost ~]# sed ‘/^#/p;s/^[^#]/#&/pg;/^#/d;s/^/#/‘ /etc/fstab # # # /etc/fstab # Created by anaconda on Wed Aug 3 06:08:10 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=1aa7af5a-15a0-4cf2-a11d-8304d67c7093 / xfs defaults 0 0 #UUID=2fb41265-5d13-4ef0-82f6-3147009bd1a5 /boot xfs defaults 0 0 #UUID=a5a6b450-ba91-4e5b-a5ad-a17e12a2a994 /testdir xfs defaults 0 0 #UUID=1d6f94c4-7eb4-430c-b143-b63b498c1c98 swap swap defaults 0 0
[root@localhost ~]# sed -r ‘s/^([^#]|$)/#&/‘ /etc/fstab # # # /etc/fstab # Created by anaconda on Wed Aug 3 06:08:10 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=1aa7af5a-15a0-4cf2-a11d-8304d67c7093 / xfs defaults 0 0 #UUID=2fb41265-5d13-4ef0-82f6-3147009bd1a5 /boot xfs defaults 0 0 #UUID=a5a6b450-ba91-4e5b-a5ad-a17e12a2a994 /testdir xfs defaults 0 0 #UUID=1d6f94c4-7eb4-430c-b143-b63b498c1c98 swap swap defaults 0 0
5、处理/etc/fstab路径,使用sed命令取出其目录名和基名
[root@localhost ~]# echo "/etc/init.d/functions" |sed ‘s/.*\<//‘ functions
[root@localhost ~]# echo "/etc/init.d/functions" |sed -r ‘s@[^/]+.?$@@g‘ /etc/init.d/
[root@localhost ~]# echo "/etc/init.d/functions" |sed -r ‘s@(.*)/([^\]+.?)$@\1@g‘ /etc/init.d [root@localhost ~]# echo "/etc/init.d/functions" |sed -r ‘s@(.*)/([^\]+.?)$@\2@g‘ functions
6、利用sed 取出ifconfig命令中本机的IPv4地址
[root@localhost ~]# ifconfig |sed -r ‘/^[^ ]/!d;N;s/^([^ ]+).*\n[^0-9]+([0-9.]+).*/\1 \2/‘ eno16777736: 10.1.70.102 lo: 127.0.0.1 virbr0: 192.168.122.1
7、统计centos安装光盘中Package目录下的所有rpm文件的以.分隔倒数第二个字段的重复次数
[root@localhost ~]# ls /mnt/Packages/ |sed -r ‘s/.*\b([^.]+)\.[^.]+$/\1/‘|sort|uniq -c 2000 i686 2938 noarch 1 TRANS 4069 x86_64
[root@localhost ~]# ls /mnt/Packages/|rev|cut -d. -f2|rev|sort|uniq -c 2000 i686 2938 noarch 1 TRANS 4069 x86_64
1、如何设置tab缩进为4个字符?
:set ts=4
2、复制/etc/rc.d/init.d/functions文件至/tmp目录;替换/tmp/functions文件中的/etc/sysconfig/init为/var/log
:%s@/etc/sysconfig/init@/var/log@g
3、删除/tmp/functions文件中所有以#开头,且#后面至少有一个空白字符的行的行首的#号
:%s/^# / /g
原文:http://11551196.blog.51cto.com/11541196/1836273