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

相關推薦

  • 馬哥教育21期網絡班—第三周課程+練習

    1、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。 who |cut -d" " -f1 |sort -u 2、取出最后登錄到當前系統的用戶的相關信息。 id `who | tail -n …

    Linux干貨 2016-07-12
  • 文本編輯sed

    *** sed:Stream EDitor  流編輯器 一次處理一行內容 sed [option]…  'script'  inputfile…常用選項:     -n∶使用安靜(silent)模式。在一般 sed 的用法中, &nbs…

    Linux干貨 2016-08-10
  • ntp時間服務器搭建實例

    ntp時間服務器采用stratum分級架構來處理時間同步;舉例說明:你搭建了一臺ntp服務器,然后同步的server為stratum-1,你的ntp則為stratum-2,你的下級ntp則為tratum-3。依此類推,最多為15層。 1.ntp server安裝: [root@localhost ~]# yum -y …

    Linux干貨 2015-11-10
  • 新的旅途

    三月末的北京已經讓人感覺有些炎熱,這是我第一次來北京。對我而言這是我人生的另一個起點,押上了我所有驕傲承載著我的夢想對未來所有的期望。這是一場豪賭,新的旅途,從今天開始。

    2018-03-26
  • Linux 第五天: (08月01日) 練習和作業

    Linux 第五天: (08月01日) 練習和作業         創建用戶gentoo, 附加組為bin和root, 默認shell為/bin/csh, 注釋信息為"Gentoo Distribution" useradd -G bin,root -c "Gentoo Distribut…

    Linux干貨 2016-08-08
  • if、case 語法

    1. 條件選擇 if 語句         選擇執行:             ? 單分支      &nbs…

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