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 14:31

相关推荐

  • vim 的使用简介

    linux day 8 使用vi和vim的三种主要模式 移动光标,进入插入模式 改变、删除、复制文本 撤销改变 搜索文档 vim寄存器 可视化和多窗口 vim帮助 vi: Visual Interface,文本编辑器 文本:ASCII, Unicode 文本编辑种类: 行编辑器: sed 全屏编辑器:nano, vi vim-Vi Improved 其他编辑…

    Linux干货 2016-08-15
  • 马哥教育网络班22期+第三周课程练习

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。  who | tr -s [[:space:]] | cut -d" " -f1 | sort -u [root@localhost cdrom]# who root     pts/1     &…

    Linux干货 2016-09-07
  • 用户管理权限命令总结

    用户 Linux操作系统是一个多用户操作系统,它允许多用户同时登录到系统上并使用资源。系统会根据账户来区分每个用户的文件,进程,任务和工作环境,使得每个用户工作都不受干扰。 用户分类 超级用户:(root,UID=0) 普通用户:(UID在500到60000) 伪用户:(UID在1到499) 系统和服务相关的:bin、daemon、shutdown等 进程相…

    Linux干货 2017-04-03
  • 逻辑卷的创建、维护和迁移

    逻辑卷管理器(LVM)介绍: 1、允许对卷进行方便操作的抽象层,包括重新设定文件系统的大小 2、允许在多个物理设备间重新组织文件系统 (1)将设备指定为物理卷 (2)用一个或者多个物理卷来创建一个卷组 (3)物理卷是用固定大小的物理区域(Physical Extent,PE)来 定义的 (4)在物理卷上创建的逻辑卷 是由物理区域(PE)组成 (5)可以在逻辑…

    Linux干货 2017-12-10
  • Docker入门

    一、Docker 架构 Docker 使用客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建Docker容器。 Docker 容器通过 Docker 镜像来创建。 容器与镜像的关系类似于面向对象编程中的对象与类。 Docker 面向对象 容器 对象 镜像 类 Docker 镜像(Images) Docker 镜像是用于创建 Docker 容器的…

    2018-01-20
  • M20-1 8月5日 文本查看工具以及文本处理工

    一、文本查看工具:  cat,tac,rev cat用法:cat [option]..[FILE]..   cat选项:    -E:显示行结束符$    -n:对显示出的每一行进行编号    -A:显示所有控制符    -b:非空行编号   &nb…

    Linux干货 2016-08-08