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.www58058.com/32140

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

相關推薦

  • Linux上文件管理類命令實例講解

    下面介紹三個文件cp, mv, rm管理命令: cp命令:copy,復制命令 命令格式: cp 源文件 目標文件 復制又分為單源復制和多源復制兩種情況: 單源復制 如果目標文件不存在,創建此文件,并復制數據流到此文件; [root@localhost tmp]# cp yum.log ok [root@localhost tmp]# ls -l total …

    Linux干貨 2018-02-28
  • 文件查找命令

    查找命令:local,find local:非實時查找,通過系統數據庫進行搜索,無法查找到在系統數據庫更新后創建的文件,但是查找速度快,模糊查找(不僅會查找到文件名還會找到文件全路徑) find:在硬盤上進行實時搜索,速度較慢,但是可以找到當前所有的數據 系統數據庫在   /var/lib/mlocate/mlocate.db 系統一般會…

    Linux干貨 2016-08-16
  • Centos 7 快速進入圖形界面

    Centos 7 快速進入圖形界面.pdf

    系統運維 2016-04-05
  • N21沉舟17周作業

    1、結合圖形描述LVS的工作原理; NAT模型 NAT模型其實就是通過網絡地址轉換來實現負載均衡的,它的工作方式幾乎跟iptables 中的DNAT一模一樣的,NAT模型的工作方式: 1.用戶請求VIP(也就是是CIP請求VIP) 2,Director Server 收到用戶的請求后,發現源地址為CIP請求的目標地址為VIP,那么Dorector Serve…

    Linux干貨 2016-11-14
  • 馬哥教育網絡班N22期+第3周課程練習

    馬哥教育網絡班N22期+第3周課程練習 1、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。 示例: [root@Red Hat Enterprise Linux Desktop]# who | cut -d' &#039…

    Linux干貨 2016-08-29
  • shell腳本編程和文件查找及壓縮

    shell腳本編程 read:使用read來把輸入值分配一個或多個shell變量     -p 指定要顯示的提示     -t TIMEOUT     read 從標準輸入中讀取值,給每個單詞分配一個變量   &nbsp…

    Linux干貨 2016-08-18
欧美性久久久久