刪除centos7系統(tǒng)/etc/grub2.cfg文件中所有以空白開頭的行行首的空白字符
sed -r 's/^[[:space:]]+//' /etc/grub2.cfg刪除/etc/fstab文件中所有以#開頭,后面至少跟一個空白字符的行的行首的#和空白字符
sed -r 's/^#[[:space:]]+//' /etc/fstab在centos6系統(tǒng)/root/install.log每一行行首增加#號
sed 's/^/#/' install.log在/etc/fstab文件中不以#開頭的行的行首增加#號
sed 's/^[^#]/#/' /etc/fstab處理/etc/sysconfig/network-scripts/路徑,使用grep和sed命令取出其目錄名和基名
echo /etc/sysconfig/network-scripts/ |grep -o '^/.*/\<'
/etc/sysconfig/
echo /etc/sysconfig/network-scripts/ |egrep -o '[^/]+/?$'
echo /etc/sysconfig/network-scripts/ |sed -r 's@^(/.*/)([^/]+/?$)@\1@'
echo /etc/sysconfig/network-scripts/ |sed -r 's@^(/.*/)([^/]+/?$)@\2@'
- 利用sed 取出ifconfig命令中本機(jī)的IPv4地址
ifconfig ens33|sed -n -e '2s/^.*et //' -e '2s/ .*$//p' - 統(tǒng)計centos安裝光盤中Package目錄下的所有rpm文件的以.分隔倒數(shù)第二個字段的重復(fù)次數(shù)
[root@centos7 Packages]#ls *.rpm|egrep -o "[^.]+\.rpm$"|sort|uniq -c
2072 i686.rpm
3044 noarch.rpm
4247 x86_64.rpm
[root@centos7 Packages]#ls *.rpm|sed -r 's@(^.*\.)([^.]+\.rpm$)@\2@'|sort|uniq -c
2072 i686.rpm
3044 noarch.rpm
4247 x86_64.rpm
- 統(tǒng)計/etc/init.d/functions文件中每個單詞的出現(xiàn)次數(shù),并排序(用grep和sed兩種方法分別實現(xiàn))
egrep -o "[[:alpha:]]+" /etc/init.d/functions|sort|uniq -c |sort -n
sed 's@[^[:alpha:]]@\n@g' /etc/init.d/functions|sed '/^$/d'|sort|uniq -c|sort -n
- 將文本文件的n和n+1行合并為一行,n為奇數(shù)行
方法一
[root@centos7 ~]#sed -n '1~2p' /etc/issue>/app/f1
[root@centos7 ~]#sed -n '2~2p' /etc/issue>/app/f2
[root@centos7 ~]#cd /app
[root@centos7 app]#paste f1 f2
\S Kernel \r on an \m
my tty is \l my hostname is \n
the time is \t
方法二
sed -n 'N;s/\n//p' f1