sed命令实战

1、删除/etc/grub2.conf文件中所有以空白开头的行行首的空白字符 

[root@centos7 ~]# 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
fi
if [ "${next_entry}" ] ; then
set default="${next_entry}"
set next_entry=
save_env next_entry
set boot_once=true
else
set default="${saved_entry}"
fi
if [ x"${feature_menuentry_id}" = xy ]; then
menuentry_id_option="--id"
else
menuentry_id_option=""
fi
export menuentry_id_option
if [ "${prev_saved_entry}" ]; then
set saved_entry="${prev_saved_entry}"
save_env saved_entry
set prev_saved_entry=
save_env prev_saved_entry
set boot_once=true
fi

2、删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首的#和空白字符 

[root@centos7 ~]# sed -r 's/^#[[:space:]]+//g' /etc/fstab
#
/etc/fstab
Created by anaconda on Tue Jul 19 14:39:24 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=f83c52ce-5740-4f02-bb87-6e190360dc30 /                       xfs     defaults  0 0
UUID=c98144e4-d1b4-45b1-bb22-3112420ea487 /boot                   xfs     defaults  0 0
UUID=dbf5c483-133e-4888-bbb1-a9622d83a930 swap                    swap    defaults  0 0
#END
[root@centos7 ~]#

2.1、删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首的#和空白字符且删除以#开头后面全是空白字符的行

[root@centos7 ~]# sed -r -e 's/^#[[:space:]]+//g' -e '/^#/d' /etc/fstab
/etc/fstab
Created by anaconda on Tue Jul 19 14:39:24 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=f83c52ce-5740-4f02-bb87-6e190360dc30 /                       xfs     defaults  0 0
UUID=c98144e4-d1b4-45b1-bb22-3112420ea487 /boot                   xfs     defaults  0 0
UUID=dbf5c483-133e-4888-bbb1-a9622d83a930 swap                    swap    defaults  0 0
[root@centos7 ~]#

3、在/etc/issue每一行行首增加#号 

[root@centos7 ~]# cat -n /etc/issue
     1\S
     2Kernel \r on an \m
     3
[root@centos7 ~]# sed -r 's@(.*)@#&@g' /etc/issue
#\S
#Kernel \r on an \m
#
[root@centos7 ~]# sed -r 's@(.*)@#\1@g' /etc/issue
#\S
#Kernel \r on an \m
#
[root@centos7 ~]# sed -r 's@^@#@g' /etc/issue
#\S
#Kernel \r on an \m
#
[root@centos7 ~]#

4、在/etc/fstab文件中不以#开头的行的行首增加#号

[root@centos7 ~]# sed -r 's/^[^#]/#/g' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue Jul 19 14:39:24 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
#
#UID=f83c52ce-5740-4f02-bb87-6e190360dc30 /                       xfs     defaults
#UID=c98144e4-d1b4-45b1-bb22-3112420ea487 /boot                   xfs     defaults
#UID=dbf5c483-133e-4888-bbb1-a9622d83a930 swap                    swap    defaults
#END
[root@centos7 ~]#

5、处理/etc/fstab路径,使用sed命令取出其目录名和基名

[root@centos7 Packages]# cd
[root@centos7 ~]# echo "/etc/hosts"|sed -r 's#/.*/([^/]+)#\1#g'
hosts
[root@centos7 ~]# echo "/etc/hosts" |sed -r 's#(/.*/)[^/]+/?#\1#g'
/etc/
[root@centos7 ~]# echo "/etc/issue"|sed -r 's#(.*/)([^/]+/?)$#\1#g'
/etc/
[root@centos7 ~]# echo "/etc/issue"|sed -r 's#(.*/)([^/]+/?)$#\2#g'
issue
[root@centos7 ~]#

6、利用sed 取出ifconfig命令中本机的IPv4地址 

[root@centos7 ~]# ifconfig |sed -n 2p
        inet 192.168.226.138  netmask 255.255.255.0  broadcast 192.168.226.255
[root@centos7 ~]# ifconfig |sed -n 2p|sed -r 's/.*net //g'
192.168.226.138  netmask 255.255.255.0  broadcast 192.168.226.255
[root@centos7 ~]# ifconfig |sed -n 2p|sed -r 's/.*net //g'|sed -r 's/  net.*//g'
192.168.226.138
[root@centos7 ~]# ifconfig |sed -nr '2s#^.*net (.*)  netm.*$#\1#gp'
192.168.226.138
[root@centos7 ~]#

7、统计centos安装光盘中Package目录下的所有rpm文件的 以.分隔倒数第二个字段的重复次数

[root@centos7 Packages]# ls *.rpm |sed -r 's/^.*\.(.*)\.rpm/\1/g'|sort |uniq -c |sort
   2000 i686
   2938 noarch
   4069 x86_64
[root@centos7 Packages]# ls *.rpm |rev |cut -d. -f2 |sort|uniq -c|uniq
   4069 46_68x
   2000 686i
   2938 hcraon
[root@centos7 Packages]#

文章总结在如下博客地址:http://purify.blog.51cto.com/10572011/1835389

原创文章,作者:alren,如若转载,请注明出处:http://www.178linux.com/32140

(0)
alrenalren
上一篇 2016-08-15 14:31
下一篇 2016-08-15

相关推荐

  • 分享我自己的一个最小化安装CentOS6的初始化脚本

    #!/bin/bash # #Filename:postinstall_init.sh #Description:系统安装完成后,对系统进行一些配置,以符合自己的试验环境 #Author:renpingsheng #Email:995883352@qq.com #Version:1.0 #Date:2017.5.5 setenforce 0 #更改selin…

    Linux干货 2017-05-07
  • 系统基础之用户,组管理作业题

    、创建testuser uid 1234,主组:bin,辅助组:root,ftp,shell:/bin/csh home:/testdir/testuser 1 2 3 [root@wen-7 ~]# useradd -u 1234 -g bin -G root,ftp&nbsp…

    Linux干货 2016-08-04
  • 系统管理和内核管理(二)之grub使用、编译安装内核

    系统管理和内核管理(二)之grub使用、编译安装内核   回顾CentOS 6启动流程: POST –> Boot Sequence(BIOS) –> Boot Loader –> Kernel(ramdisk) –> rootfs–> switchroot&…

    Linux干货 2016-09-15
  • find命令使用练习

    1、  查找/var目录下属主为root,且属组为mail的所有文件   2、  查找/var目录下不属于root、lp、gdm的所有文件   3、  查找/var目录下最近一周内其内容修改过,同时属主不为root,也不是postfix的文件   4、  查找当前系统上没有属主或属组,且最…

    Linux干货 2016-08-15
  • 【26期】Linux第六周学习小总结

        时光匆匆,一周时光悠然而逝,带给我们的只有知识的充实,和每日强大的自信心,学习的日益深入,慢慢的然我们的思绪和状态带回高中时光,吃饭上课睡觉三点一线的生活,枯燥中透露出稍微的晨曦,让每个明天都充满着期待,那我们的本周的学习中磁盘管理算是占了很大的比重,毕竟关于磁盘的知识很是难理解,那我就把本周的知识回顾温习,同时分享给大家。 &n…

    2017-08-19
  • Linux发行版之间的联系与区别及其哲学思想。

    一、Linux发行版之间的联系与区别。 随着Linux不断发展,Linux所支持的文件系统类型也在迅速扩充。很多的数据中心服务器上都运行着Linux,可以节省大量的许可证费用及维护费用。但伴随着Linux新版本的发行,其中每一个不同版本的Linux所支持的文件系统类型都有所不同。Linux的发行版有数百种之多。 1、Debian:是指一个致力于创建自由操作系…

    Linux干货 2016-10-30